Creating SharePoint items with CreateItemActivity

Instead of creating new list items from code in your workflow you can use CreateItemActivity. CreateItemActivity can be used for creating list items or files. Properties of interest for this activity are the following (most of them are self-explanatory):

Property Name

Description

__Context

WorkflowContext object.

ItemProperties

Properties of a new item.

ListId

List ID or list name where new item will be created.

NewItemId

New item ID is stored in this property.

Overwrite

Set to true if you want to overwrite existing items.

In order to use CreateItemActivity (or any other *ItemActivity from the toolbox such as CheckInItemActivity, CopyItemActivity, etc.), first you need to add ApplyActivation activity to the workflow. With ApplyActivation activity we want to initialize the WorkflowContext object and then bind it to __Context property of CreateItemActivity.

Let’s start by creating a new SharePoint 2010 Sequential Workflow project. In the wizard, choose List workflow and Announcements list. Once project is created, drag and drop ApplyActivation activity and CreateItemActivity to the designer.

Select ApplyActivation activity and bind the __Context property to a new field (or property). To open the binding dialog, click (…) next to __Context property in the properties window. In the binding dialog, click on Bind to a new member tab and click OK to create the binding (see figure below).

Repeat the same steps for __WorkflowProperties property, but instead of creating a new field/property, bind it to workflowProperties variable in the Bind to an existing member tab. For correct property bindings, refer to the figure below.

Now that we have the workflow context, we can move to CreateItemActivity. Bind the __Context property to workflowContext variable created before. Next, bind the ListId property to ListId property of workflowContext.

Next, we want to specify properties for the new item. In order to create new items, we need to pass in the ItemProperties to the activity in a form of a Hashtable. This Hashtable contains list field names and their desired values. Follow the steps below to add a hashtable and set the values:

  1. Right click on the CreateItemActivity property in the designer.
  2. Select Bind Property 'ItemProperties'.
  3. Create a new member for this property (e.g. itemProperties).
  4. Click OK to close the dialog.
  5. Double click onWorkflowActivated1 activity (we are going to use this activity to setup the item properties).
  6. Once in code view add the following code:
 itemProperties = new Hashtable (); 
itemProperties.Add("Title", "My New Item Title"); 

Check the two figures below for property bindings on CreateItemActivity and finished workflow in the designer.

You can press F5 at this point and verify new item is created once you start the workflow. Other *ItemActivity activities work the same way - use ApplyActivation for workflow context, set the properties on *ItemActivity and you are all set.

Peter Jausovec