chrisg

Multiple SharePoint List items in one event

In a few internal discussions as well as with questions from you the community we have run into situations where someone might want to add multiple list items to a SharePoint list in one SharePoint event. So some of our super fantastic K2 APAC team members came up with a good example of how to do it which I am sharing here.

This code will let you know which areas of the SharePoint List wizard code you need to change in order to do insertion of multiple list items in one SharePoint List create list event (current limitation). Without this change of code, our current implementation will create a single SharePoint list item and concatenate the values in the repeating nodes. To give you an example we attached a InfoPath form with a repeating node, as well as a code snippet which shows the changes that needs to be made. I have also attached a XML example of what our webservices is expecting (the parameter "createItemsXML") would look like. This will help you debug your own scenario and verify the XML that is passed in is correct.

IP Repeating NodeCode

private void CreateListItem_ExecuteCode(object sender, EventArgs e)

{

//Changes here – Put your XML field name

string ipXMLFieldName = "Repeating";

//Changes here – Replace with xpath to repeating node

string repeatingNodeXPath = "/my:myFields/my:Assets";

XmlDocument xmlDocument = new XmlDocument();

XmlManipulation xmlManipulation = new XmlManipulation();

string SPURL = "";

string returnIDs = "";

// Changes here to parse the List XML and create it

xmlDocument.LoadXml(GetXMLValue());

XmlNodeList createItems = xmlDocument.SelectNodes("Items/Item");

string resultSet = "<Items>";

foreach (XmlNode createItem in createItems)

{

if (string.IsNullOrEmpty(ipXMLFieldName))

{

resultSet += "<Item>";

resultSet += "<SiteUrl>" + createItem.SelectSingleNode("SiteUrl").InnerText + "</SiteUrl>";

resultSet += "<ListName>" + createItem.SelectSingleNode("ListName").InnerText + "</ListName>";

resultSet += "<ListFolder>" + createItem.SelectSingleNode("ListFolder").InnerText + "</ListFolder>";

resultSet += "<FileMetaData>" + createItem.SelectSingleNode("FileMetaData").InnerXml + "</FileMetaData>";

resultSet += "</Item>";

}

else

{

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(General.GetEncodedXMLString(K2.ProcessInstance.XmlFields[ipXMLFieldName].Value, "ListURL"));

XmlNamespaceManager xNSM = new XmlNamespaceManager(xmlDoc.NameTable);

xNSM.AddNamespace("my", xmlDoc.DocumentElement.NamespaceURI);

XmlNodeList items = xmlDoc.SelectNodes(repeatingNodeXPath, xNSM);

foreach (XmlNode selectedNode in items)

{

resultSet += "<Item>";

resultSet += "<UniqueID>" + Guid.NewGuid() + "</UniqueID>";

resultSet += "<SiteUrl>" + createItem.SelectSingleNode("SiteUrl").InnerText + "</SiteUrl>";

resultSet += "<ListName>" + createItem.SelectSingleNode("ListName").InnerText + "</ListName>";

resultSet += "<ListFolder>" + createItem.SelectSingleNode("ListFolder").InnerText + "</ListFolder>";

resultSet += "<ContentType>" + createItem.SelectSingleNode("ContentType").InnerText + "</ContentType>";

resultSet += "<IsPartOfProcess>False</IsPartOfProcess><ProcessFieldName></ProcessFieldName><UseXmlField>False</UseXmlField>";

resultSet += "<FileMetaData>";

resultSet += "<Fields>";

// Changes here to parse the Repeating Nodes

resultSet += BuildMetaDataNode("Title", "Title", "Text", selectedNode.SelectSingleNode("my:Title", xNSM).InnerText);

resultSet += BuildMetaDataNode("PersonName", "PersonName", "Text", selectedNode.SelectSingleNode("my:PersonName", xNSM).InnerText);

// An example of how you set for other column types in SharePoint List

//resultSet += BuildMetaDataNode("Status", "Status", "Choice", selectedNode.SelectSingleNode("my:Status", xNSM).InnerText);

//resultSet += BuildMetaDataNode("% Complete", "PercentComplete", "NumberPercentage", selectedNode.SelectSingleNode("my:PercentComplete, xNSM").InnerText);

//resultSet += BuildMetaDataNode("Assigned To", "AssignedTo", "User", selectedNode.SelectSingleNode("my:AssignedTo, xNSM").InnerText);

//resultSet += BuildMetaDataNode("Description", "Body", "Note", selectedNode.SelectSingleNode("my:Description, xNSM").InnerText);

//resultSet += BuildMetaDataNode("Start Date", "StartDate", "DateTime", selectedNode.SelectSingleNode("my:StartDate, xNSM").InnerText);

//resultSet += BuildMetaDataNode("Due Date", "DueDate", "DateTime", selectedNode.SelectSingleNode("my:DueDate, xNSM").InnerText);

//resultSet += BuildMetaDataNode("Content Type", "Content Type", "", selectedNode.SelectSingleNode("my:ContentType,xNSM").InnerText);

resultSet += "</Fields>";

resultSet += "</FileMetaData>";

resultSet += "</Item>";

}

}

}

resultSet += "</Items>";

string createItemsXML = xmlManipulation.GroupXml(resultSet, "Item", "SiteUrl");

//This is the original createItems XML Code, comment off

//string createItemsXML = xmlManipulation.GroupXml(GetXMLValue(), "Item", "SiteUrl");

xmlDocument.LoadXml(createItemsXML);

foreach (XmlNode node in xmlDocument.FirstChild.ChildNodes)

{

SPURL = node.FirstChild.InnerText.ToString();

if (SPURL != string.Empty)

{

_k2SPListItems.Url = General.FormatSiteURL(SPURL) + "_vti_bin/K2SPListItems.asmx";

_k2SPListItems.Credentials = _adCredentials.GetCredentials(SPURL);

returnIDs = _k2SPListItems.CreateListItemFromXml(node);

xmlDocument.LoadXml(General.GetEncodedXMLString(returnIDs, "ListName"));

XmlNodeList returnIDItems = xmlDocument.SelectNodes("Items/Item");

foreach (XmlNode returnIDNode in returnIDItems)

{

if (bool.Parse(returnIDNode.SelectSingleNode("IsPartOfProcess").InnerText))

{

MaintainProcessXmlField(

returnIDNode.SelectSingleNode("SiteURL").InnerText,

returnIDNode.SelectSingleNode("ListName").InnerText,

returnIDNode.SelectSingleNode("ListFolder").InnerText,

returnIDNode.SelectSingleNode("ID").InnerText,

returnIDNode.SelectSingleNode("ProcessFieldName").InnerText);

}

if (!returnIDNode.SelectSingleNode("ItemIDFieldName").InnerText.Equals(""))

{

PopulateK2Field(

returnIDNode.SelectSingleNode("ID").InnerText,

(K2FieldTypes)Enum.Parse(typeof(K2FieldTypes), returnIDNode.SelectSingleNode("ItemIDFieldType").InnerText),

(K2FieldScopeTypes)Enum.Parse(typeof(K2FieldScopeTypes), returnIDNode.SelectSingleNode("ItemIDFieldScope").InnerText),

returnIDNode.SelectSingleNode("ItemIDFieldName").InnerText,

returnIDNode.SelectSingleNode("ItemIDFieldXPath").InnerText);

}

}

}

}

}

// Add this method as well into the code region "Private Methods"

private string BuildMetaDataNode(string itemTitle, string itemName, string itemType, string itemValue)

{

return "<Field><Title>" + itemTitle + "</Title><Name>" + itemName + "</Name><Type>" + itemType + "</Type><Value>" + itemValue + "</Value></Field>";

}

Example Result XML

<Items>

<Item>

<UniqueID>0f8f7f66-28a7-4d44-bbd3-16e41e9e22d9</UniqueID>

<SiteUrl>http://jacovdwtecra/Docs</SiteUrl>

<ListName>Tasks</ListName>

<ListFolder></ListFolder>

<ContentType>Task</ContentType>

<IsPartOfProcess>False</IsPartOfProcess>

<ProcessFieldName></ProcessFieldName>

<UseXmlField>False</UseXmlField>

<FileMetaData>

<Fields>

<Field>

<Title>Title</Title>

<Name>Title</Name>

<Type>Text</Type>

<Value>Item One</Value>

</Field>

<Field>

<Title>Priority</Title>

<Name>Priority</Name>

<Type>Choice</Type>

<Value>(2) Normal</Value>

</Field>

<Field>

<Title>Status</Title>

<Name>Status</Name>

<Type>Choice</Type>

<Value>Not Started</Value>

</Field>

<Field>

<Title>% Complete</Title>

<Name>PercentComplete</Name>

<Type>NumberPercentage</Type>

<Value></Value>

</Field>

<Field>

<Title>Assigned To</Title>

<Name>AssignedTo</Name>

<Type>User</Type>

<Value></Value>

</Field>

<Field>

<Title>Description</Title>

<Name>Body</Name>

<Type>Note</Type>

<Value></Value>

</Field>

<Field>

<Title>Start Date</Title>

<Name>StartDate</Name>

<Type>DateTime</Type>

<Value>Today's Date</Value>

</Field>

<Field>

<Title>Due Date</Title>

<Name>DueDate</Name>

<Type>DateTime</Type>

<Value></Value>

</Field>

<Field>

<Title>Content Type</Title>

<Name>Content Type</Name>

<Type></Type>

<Value>Task</Value>

</Field>

</Fields>

</FileMetaData>

<MetaDataXmlFieldName></MetaDataXmlFieldName>

</Item>

<Item>

<UniqueID>0f8f7f66-28a7-4d44-bbd3-16e41e9e22d6</UniqueID>

<SiteUrl>http://jacovdwtecra/Docs</SiteUrl>

<ListName>Tasks</ListName>

<ListFolder></ListFolder>

<ContentType>Task</ContentType>

<IsPartOfProcess>False</IsPartOfProcess>

<ProcessFieldName></ProcessFieldName>

<UseXmlField>False</UseXmlField>

<FileMetaData>

<Fields>

<Field>

<Title>Title</Title>

<Name>Title</Name>

<Type>Text</Type>

<Value>Item Two</Value>

</Field>

<Field>

<Title>Priority</Title>

<Name>Priority</Name>

<Type>Choice</Type>

<Value>(2) Normal</Value>

</Field>

<Field>

<Title>Status</Title>

<Name>Status</Name>

<Type>Choice</Type>

<Value>Not Started</Value>

</Field>

<Field>

<Title>% Complete</Title>

<Name>PercentComplete</Name>

<Type>NumberPercentage</Type>

<Value></Value>

</Field>

<Field>

<Title>Assigned To</Title>

<Name>AssignedTo</Name>

<Type>User</Type>

<Value></Value>

</Field>

<Field>

<Title>Description</Title>

<Name>Body</Name>

<Type>Note</Type>

<Value></Value>

</Field>

<Field>

<Title>Start Date</Title>

<Name>StartDate</Name>

<Type>DateTime</Type>

<Value>Today's Date</Value>

</Field>

<Field>

<Title>Due Date</Title>

<Name>DueDate</Name>

<Type>DateTime</Type>

<Value></Value>

</Field>

<Field>

<Title>Content Type</Title>

<Name>Content Type</Name>

<Type></Type>

<Value>Task</Value>

</Field>

</Fields>

</FileMetaData>

<MetaDataXmlFieldName></MetaDataXmlFieldName>

</Item>

</Items>

I hope this helps someone!

Published Tuesday, April 22, 2008 6:07 PM by chrisg

Attachment(s): Repeating.xsn

Comments

No Comments
Anonymous comments are disabled

About chrisg

I am responsible for community development for SourceCode. I have been in technology for over 14 years mainly in infrastructure and security. I absolutely love technology especially new stuff and gadgets.