Create, Update, Delete and Copy Item Activity in SharePoint 2010 Workflow

This article explains how to use create, update, delete and copy an Item activity that is available in the Workflow Toolbox to create and update an existing item or to delete and copy an item from one document to another in the Document Library in a SharePoint List using Visual Studio 2010.

Part 1: Create Item Activity in SharePoint 2010 Workflow
 
In this part , I will explain how to use the Create Item Activity available in the Workflow Toolbox to create a new item in a SharePoint List using Visual Studio 2010.
 
Step-by-step Implementation
  1. Create a Custom List in the SharePoint site as WFCreateItem
  2. Create an Empty SharePoint project in Visual Studio 2010 and provide the name as CreateItemActivity and click OK.



  3. In the next screen provide the site collection URL and choose Deploy as Farm solution and click Finish.
  4. Right-click on the project and add new item as Sequential Workflow and provide the name as WFCreateItemActivity



  5. Click the Add button and provide the name as WF Create Item Activity and choose List Workflow option as shown below.



  6. Click Next and associate the list to WFCreateItem that we created in Step 1.



  7. Click Next and unselect the first option as below and click Finish.



  8. Then you will see the Workflow Designer screen as below.



  9. Select onWorkflowActivated1 activity and press F4 to open the properties and change the name to onWFActivated.
  10. Drag and drop LogToHistoryListActivity to the designer and change the name as logWFStarted in the properties window.



  11. Then set the History Description property as WF Started.
  12. Drag and drop the Apply Activation control into the workflow designer after logWFStarted activity and do the procedure below (apply the activation activity that will initialize the workflow context object and then bind into the __Context property of ItemActivity).



    • Change the name to applyActivation
    • Bind a new property to the __Context property. Click on the button next to the __Context property and select the new member tab and create the variable called workflowContext.



    • Bind the existing property to __WorkflowProperties. Click on the button next to the __WorkflowProperties and select the existing member tab and select workflowProperties as shown below.



    • Check the final properties screen as below.


       
  13. Drag and drop the CreateItemActivity to the designer from the toolbox and provide the name as createNewItem.



  14. Bind the __Context property to the existing workflowContext property as shown below.



  15. Then Bind the ListId property to the ListId property of workflowContext as shown below.



  16. Select ItemProperties from the properties window for the createNewItem activity.


  17. Right-click on CreateItemActivity and select “Bind Property ItemProperties…” and create a new member as itemProperties.





  18. Drag and drop 2 LogToHistoryActivities to the designer and provide the name as logItemCreated and logWFCompleted respectively. Then provide the History Description as New Item Added Using WF and WF Completed respectively.
  19. Then the final UI is as below.



  20. Right-click on the onWFActivated activity and select Generate Handlers as shown below.



  21. Add the following code inside the method:



  22. Change the feature name to WFCreateItemActivityFeature and provide the description as shown below.



  23. Build and deploy the project.
  24. Go to the SharePoint site and navigate to the list that you have deployed the project.
  25. Add a new item to the list and see that the new item has been created as shown below.



  26. Click on Completed and you will see the workflow history as shown below.


 

Part 2: Update Item Activity in SharePoint 2010 Workflow

In this part I will explain how to use update an Item Activity that is available in the Workflow Toolbox to update an existing item in a SharePoint List using Visual Studio 2010.

Step-by-step Implementation

  1. Create a Custom List in a SharePoint site as WFUpdateItem.
  2. Create an empty SharePoint project in Visual Studio 2010 and provide the name as UpdateItemActivity and click OK.



  3. In the next screen provide the site collection URL and choose Deploy as Farm solution and click Finish.
  4. Right-click on the project and add a new item as Sequential Workflow and provide the name as WFUpdateItemActivity.



  5. Click the Add button and provide the name as WF Update Item Activity and choose the List Workflow option as shown below.



  6. Click Next and associate the list to WFUpdateItem that we created in Step 1.



  7. Click Next and unselect the first option as below and click Finish.



  8. Then you will see the Workflow Designer screen as below.



  9. Select the onWorkflowActivated1 activity and press F4 to open the properties and change the name to onWFActivated.
  10. Drag and drop LogToHistoryListActivity to the designer and change the name to logWFStarted in the properties window.



  11. Then set the History Description property to WF Started.
  12. Drag and drop the Apply Activation control into the Workflow Designer after the logWFStarted activity and do the procedure below (apply the activation activity will initialize the workflow context object and then bind into __Context property of ItemActivity).



    • Change the name to applyActivation
    • Bind a new property to the __Context property. Click on the button next to the __Context property and select the new member tab and create the variable called workflowContext.



    • Bind the existing property to __WorkflowProperties. Click on the button next to the __WorkflowProperties and select the existing member tab and select workflowProperties as shown below.



    • Check the final properties screen as below.



  13. Drag and drop the UpdateItemActivity to the designer from the toolbox and provide the name as updateExistingItem



  14. Bind the __Context property to the existing workflowContext property as shown below.



  15. Then Bind the ListId property to the ListId property of workflowContext as shown below.



  16. Select ItemProperties from the properties window for updateExistingItem activity.



  17. Right-click on UpdateItemActivity and select “Bind Property ItemProperties…” and create a new member as itemProperties.





  18. Bind a new property to the ListItem property. Click on the button next to the ListItem property and select the new member tab and create the variable called listItem.
  19. Drag and drop 2 LogToHistoryActivity to the designer and provide the name as logItemUpdated and logWFCompleted respectively. Then provide the History Description as Item Updated Using WF and WF Completed respectively.
  20. Then the final UI as below.



  21. Right-click on the onWFActivated activity and select Generate Handlers as shown below.



  22. Add the following code inside the method.



  23. Change the feature name as WFUpdateItemActivityFeature and provide the description as shown below.



  24. Build and deploy the project.
  25. Go to the SharePoint site and navigate to the list that you have deployed the project.
  26. Add a new item to the list as shown below.



  27. Then refresh the page to see that the list item has been updated as shown below.



  28. Click on Completed and you will see the workflow history as shown below.


 

Part 3: Delete Item Activity in SharePoint 2010 Workflow

In this part, I will explain how to use Delete Item Activity that is available in Workflow Toolbox to delete existing item in a SharePoint List using Visual Studio 2010.

Step-by-step Implementation

  1. Create a Custom List in a SharePoint site as WFDeleteItem and WFListItems.
  2. Add some dummy data in the WFListItems list as shown below. Change the view with ID.



  3. Create an Empty SharePoint project in Visual Studio 2010 and provide the name as DeleteItemActivity and click OK.



  4. In the next screen provide the site collection URL and choose Deploy as Farm solution and click Finish.
  5. Right-click on the project and add a new item as Sequential Workflow and provide the name as WFDeleteItemActivity.



  6. Click the Add button and provide the name as WF Delete Item Activity and choose List Workflow option as shown below.



  7. Click Next and associate the list to WFDeleteItem that we created in Step 1.



  8. Click Next and unselect the first option as below and click Finish.



  9. Then you will see the Workflow Designer screen as below.



  10. Select the onWorkflowActivated1 activity and press F4 to open the properties and change the name to onWFActivated.
  11. Drag and drop LogToHistoryListActivity to the designer and change the name as logWFStarted in the properties window.



  12. Then set the History Description property as WF Started.
  13. Drag and drop the Apply Activation control into the workflow designer after the logWFStarted activity and do the procedure below (apply the activation activity will initialize the workflow context object and then bind into the __Context property of ItemActivity).



    • Change the name to applyActivation.
    • Bind a new property to the __Context property. Click on the button next to the __Context property and select the new member tab and create the variable called workflowContext.



    • Bind the existing property to __WorkflowProperties. Click on the button next to the __WorkflowProperties and select the existing member tab and select workflowProperties as shown below.



    • Check the final properties screen as below.



  14. Drag and drop the DeleteItemActivity to the designer from the toolbox and provide the name as deleteExistingItem.



  15. Bind the __Context property to an existing workflowContext property as shown below.



  16. Bind a new property to the ListId property. Click on the button next to the ListId property and select the new member tab and create the variable called ListId.



  17. Bind a new property to the ListItem property. Click on the button next to the ListItem property and select the new member tab and create the variable called listItem.



  18. Drag and drop 2 LogToHistoryActivities to the designer and provide the name as logItemDeleted and logWFCompleted respectively. Then provide the History Description as Item Deleted Using WF and WF Completed respectively.
  19. Then the final UI is as below.



  20. Right-click on the onWFActivated activity and select Generate Handlers as shown below.



  21. Add the following code inside the method:



  22. Change the feature name WFDeleteItemActivityFeature and provide the description as shown below.



  23. Build and deploy the project.
  24. Go to the SharePoint site and navigate to the list that you have deployed the project.
  25. Add a new item to the list.



  26. Then refresh the page and you will see the output as shown below.



  27. Click on Completed and you will see the workflow history as shown below.



  28. Click on the WFListItems list and see the item that has the ID 4 has been deleted from the list.



 

Part 4: Copy Item Activity in SharePoint 2010 Workflow

In this part, I will explain how to use Copy Item Activity that is available in Workflow Toolbox to copy an item from one document to another Document Library using Visual Studio 2010.

Step-by-step Implementation

  1. Create a Document Library in the SharePoint site as WF Documents and Client Documents.
  2. Create an empty SharePoint project in Visual Studio 2010 and provide the name as CopyItemActivity and click OK.



  3. In the next screen provide the site collection URL and choose Deploy as Farm solution and click Finish.
  4. Right-click on the project and add a new Sequential Workflow item and provide the name as WFCopyItemActivity.



  5. Click the Add button and provide the name as WF Copy Item Activity and choose List Workflow option as shown below.



  6. Click Next and associate the list to WF Documents that we created in Step 1.



  7. Click Next and unselect the first option as below and click Finish.



  8. Then you will see the Workflow Designer screen as below.



  9. Select the onWorkflowActivated1 activity and press F4 to open the properties and change the name to onWFActivated.
  10. Drag and drop LogToHistoryListActivity to the designer and change the name to logWFStarted in the properties window.



  11. Then set the History Description property as WF Started.
  12. Drag and drop the Apply Activation control into the Workflow Designer after the logWFStarted activity and do the procedure below (apply the activation activity will initialize the workflow context object and then bind into the __Context property of ItemActivity).



    • Change the name to applyActivation.
    • Bind a new property to the __Context property. Click on the button next to the __Context property and select the new member tab and create the variable called workflowContext.



    • Bind the existing property to __WorkflowProperties. Click on the button next to the __WorkflowProperties and select the existing member tab and select workflowProperties as shown below.



    • Check the final properties screen as below.


     
  13. Drag and drop the CopyItemActivity to the designer from the toolbox and provide the name as copyItemToLibrary.



  14. Bind the __Context property to an existing workflowContext property as shown below.



  15. Bind the ListId property to the ListId property of workflowContext as shown below:



  16. Bind a new property to the ListItem property. Click on the button next to the ListItem property and select the new member tab and create the variable called listItem.



  17. Bind a new property to the ToListId property. Click on the button next to the ToListId property and select the new member tab and create the variable called listId.



  18. Drag and drop 2 LogToHistoryActivities to the designer and provide the name as logItemCopied and logWFCompleted respectively. Then provide the History Description as Item Copied to Destination Using WF and WF Completed respectively.
  19. Then the final UI is as below.



  20. Right-click on the onWFActivated activity and select Generate Handlers as shown below.



  21. Add the following code inside the method:


  22. Change the feature name to WFCopyItemActivityFeature and provide the description as shown below.



  23. Build and deploy the project.
  24. Go to the SharePoint site and navigate to the library that you have deployed the project.
  25. Upload a new document to the library.



  26. Then refresh the page and you will see the output as shown below.



  27. Click on Completed and you will see the workflow history as shown below.



  28. Click on the Client Documents library and see that the file is successfully uploaded to the library.



  29. That's all.