Insert Forms data
It's quite straightforward to create new Forms records:
- Read the Forms tool's schema into a DataSet;
- Add new records to the DataSet with appropriate field values;
- Call the Forms CreateRecords() method to insert the new records.
A single call to CreateRecords() will insert as many new records as contained in your dataset; you don't need to make a separate call for each one. (It's quite a lot more efficient to create multiple records at once).
Here's the code I use to make an Insert() method in my wrapper class. It takes a System.Data.DataSet, writes its schema and data to the GrooveForms2WebService RecordDataSet, and calls the CreateRecords() method on the web service:
public string[] Insert(System.Data.DataSet dataSet)
{
if (!IsDesignInitialized)
{
throw new Exception("Forms tool design is not initialized.");
}
GrooveForms2WebService.Forms2RecordDataSet formsDs = new GrooveForms2WebService.Forms2RecordDataSet();
dataSet.AcceptChanges();
string str = dataSet.GetXmlSchema();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(str);
XmlElement[] schemaArray = new XmlElement[1];
schemaArray[0] = xmlDoc.DocumentElement;
formsDs.Schema = schemaArray;
str = dataSet.GetXml();
xmlDoc.LoadXml(str);
XmlNodeList nodelist = xmlDoc.DocumentElement.ChildNodes;
XmlElement[] dataArray = new XmlElement[nodelist.Count];
int j = 0;
foreach (XmlNode node in nodelist)
{
dataArray[j] = (XmlElement)node;
j++;
}
formsDs.Data = dataArray;
GrooveForms2WebService.GrooveForms2 svc = GetDataService();
return svc.CreateRecords(formsDs);
}
The return value is an array of RecordURIs of the newly-created records.
To call this, here's an example setting text fields to random values, in 100 new records:
GrvFormsTool tool = new GrvFormsTool( . . . .);
// Construct a DataSet matching the tool's schema
DataSet ds = tool.GetSchema();
if (ds.Tables.Count > 0)
{
// First table in the DataSet corresponds to the first form
// (you should usually open the table by name...!)
DataTable dt = ds.Tables[0];
// Insert 100 records with text fields set to random values
for (int j = 0; j < 100; j++)
{
DataRow dr = dt.NewRow();
foreach (DataColumn c in dt.Columns)
{
if (!ReservedFormsFields.Contains(c.ColumnName))
{
if (c.DataType is String)
{
dr[c] = Guid.NewGuid().ToString();
}
}
}
dt.Rows.Add(dr);
}
tool.Insert(ds);
}
Of course I'm expecting you already know which fields you want to set.
Note that the record's schema includes several "internal" fields, which you generally don't want to write values for (they'll be assigned automatically by Groove as appropriate). Some of these fields are writable in some circumstances: _CreatedByURL can be set when a new record is created, for example, but is read-only thereafter. There's more documentation in the SDK. In this example I've wrapped this list of reserved fields in a small static helper property:
private static string[] _excl = {
"RecordURI",
"_RecordID",
"_ParentID",
"_Modified",
"_ModifiedBy",
"_ModifiedByURL",
"_Created",
"_CreatedBy",
"_CreatedByURL",
"_Editors",
"_Readers",
"_IgnoreUnread",
"_UnreadFlag",
"Forms_Tool_grooveFormID",
"Forms_Tool_IPContents" };
private static List<string> ReservedFormsFields
{
get
{
return new List<string>(_excl);
}
}