Thursday, May 12, 2016

Creating a Folder in a SharePoint List and adding Items to it programatically

The following code is to create a folder in a SharePoint list and add items to it. This code is written for a Custom Visual Studio Workflow, the workflow will be started when a new item is added to a list.

public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
SPList list = null;
SPListItem folderItem = null;

private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
 SPListCollection listCollection = workflowProperties.Web.Lists;

//Creating folder in "MyContacts" Lists
list = listCollection["MyContacts"];

// create a folder under the path specified
folderItem = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);

// set the folder name and update
folderItem["Title"] = "My Folder"

//create a listitem object to add item in the foler
SPListItem listItem = list.Items.Add(folderItem.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);

//Set the values for other fields in the list
listItem["Contact ID"] = workflowProperties.Item["Contact ID"];
listItem["Contact Name"] = workflowProperties.Item["Contact ID"];
oWeb.AllowUnsafeUpdates = false;

No comments:

Post a Comment