Condividi tramite


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

Comments

  • Anonymous
    April 15, 2011
    Useless post... doesnt work. Dont waste your time like I did!

  • Anonymous
    July 27, 2011
    It is very useful It is worked fine thank you I need to create new item with current data ! can I do that ? thank you for your help

  • Anonymous
    June 06, 2012
    if the workflow gets put to sleep the workflow context property is no longer available. you would have to call a new apply activity each time you want to use it. and there is still no guarantee that it won't be put to sleep during that time. so it's probably not going to work for any but the simplest of workflows (as shown here)

  • Anonymous
    February 28, 2013
    post if very useful if you understand workflow foundation its not going to build itself lazy people

  • Anonymous
    April 24, 2013
    Hi I'm using CopyItemActivity, In the properties what do i put in the  ListItem and ToListId? Will this activity retain list item version? Thanks

  • Anonymous
    May 13, 2013
    i need to update item ,try it ,but is wrong. maybe the listitem property is wrong. how can i set the property ?

  • Anonymous
    August 27, 2013
    Thank you, very helpful

  • Anonymous
    July 28, 2014
    Not able to set  __WorkflowProperties.its giving error like "currently seleted property has wrong type....." what is going wrong????