Wednesday 16 March 2016

Creating Custom Code Activity and Using it in Visual Studio 2013 Workflows For SharePoint 2013

This article explains how to create a Custom Code Activity and how to use it in Visual Studio 2013 Workflows for SharePoint 2013.

Implementation Procedure
  1. Open Visual Studio 2013 and create a new project as Activity Library from the Workflow category and provide the name as CustomListCreation.


  2. Click Ok. Then right-click on Activity1.xaml and select delete to remove the XAML file, since we are going to create a code activity.
  3. Right-click on the CustomListCreation project and add a new item as Code Activity from the workflow installed category and provide the name as CustomListCodeActivity.
  4. Then you will see the following code in the CustomListCodeActivity.cs file.


  5. Add a reference as Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll from the ISAPI folder (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI)
  6. To allow default qualification of the Microsoft.SharePoint.Client namespace use the following directive:
    using Microsoft.SharePoint.Client;
  7. Then create the variable as in the following screenshot.


  8. Then write the code inside the Execute method.




  9. Right-click on the project and select Properties and do the following procedure.

    • Select Signing
    • Check the Sign the assembly checkbox
    • Choose New from the drop down
        
  10. Then provide the Key file name as CustomListKey and uncheck the Protect my key file as in the following:


  11. Save the project and close the properties window.
  12. Build the project and you will see the Build succeeded message.
  13. Right-click on the project and add a new item as XML File under Data category and provide the name as AllowedTypes.xml.


  14. Type the following code in the XML file.


  15. Next we will do the deployment.

    • Copy CustomListCreation.dll from the debug folder and paste into the following location.


    • Copy the AllowedTypes.xml file from the project folder and paste into the above two locations.
    • Open a command prompt to restart the “Workflow Manager Backend” service.
    • Type net stop “Workflow Manager Backend” and you will see the message as below.

    • Then type net start “Workflow Manager Backend” and you will see the message as below.


    • DeployCustomListCreation.dll file into GAC using gacutil.exe
    • Open Visual Studio command prompt and type the following command to execute it.

      gacutil /i “C:\FolderName for Project\bin\debug\CustomListCreation.dll” /f
       
    • Then you will see the output as shown below.

    • Then reset IIS. Type the command as iisrest in the command prompt.
     
  16. Next we will use this custom code activity in the project.
  17. Open the site you want to deploy it and create the list you want (in this example, “WFCodeActivityList” is created).
  18. Right-click on the project solution and select "New Project…."
  19. Select "SharePoint 2013 – Empty Project" and provide the name as “WFCustomList”.
  20. Provide the SharePoint URL and select sandboxed solution and click Finish.
  21. Right-click on the newly created SharePoint project and add a workflow and new item as shown below.


  22. Provide the workflow name as WF List Code Activity in the next window and select List Workflow as below.


  23. Click Next and select the list we created in the step 17 from the drop down list.


  24. In the history list drop down, select WorkflowHistoryList or select <Create New>
  25. In the task list drop down, select WorkflowTaskList or select <Create New>. Then the final UI is shown below.


  26. Click Next and leave it as default selection and click Finish
  27. Then you will see the designer window and you will see the custom activity in the toolbox as shown below.


  28. Drag and drop 3 WriteToHistory activities to the Sequence from the toolbox under the SP – Utilities category.
  29. Select the first WriteToHistory activity from the designer and press F4 to open the properties window and click on the (…) button next to Message. 


  30. Then provide the message as “WF Started” in the Expression Editor window and click Ok.


  31. Repeat Steps 29 and 30 for the third WriteToHistory activity and provide the message as “WF Completed”.
  32. Drag and drop the CustomListCodeActivity from the CustomListCreation category between the first two WriteToHistory activities and the final UI is shown below.


  33. Then select Sequence from the designer and click on Variables to create a new variable.


  34. In the variable window create a new variable called outputMessage and set the variable type as string.


  35. Then again click on Variables as we did in the step 33 to close the variable window.
  36. Select the second WriteToHistory activity from the designer and provide the message as string.Format("Output Message : {0}", outputMessage) – Refer step 29 and 30
  37. Select CustomListCodeActivity and open the properties window and do the following procedure.

    • Provide the value for ListDescription as "My Custom Task List Using WF"
    • Provide the value for ListName as "TaskListUsingWF"
    • Provide the value for ListTemplateName as "Tasks"
    • Provide the value for OutMessage as outputMessage (Assign the variable)
    • Provide the SiteURL value as 


    • Final properties for CustomListCodeActivity

     
  38. Rename the feature and deploy the project.
  39. Then you will see the deployment success message in the output window.


  40. Then go to the List you have associated the workflow and add new item as “My Item”.


  41. Click on (…) and select workflows from the list. Since the WF is started manually.


  42. Click on the link as shown below to start the workflow.

  43. Again repeat the step 41 and click Started as shown below.


  44. Refresh the page for a few seconds and you will see the workflow history as shown below.


  45. Then you will see the custom list created by the workflow is displayed in the Quick Launch as shown below.


  46. Verify the list name is same as we provided in the step 37.
  47. That's all…. You have done the custom code activity workflow in SharePoint 2013.

Create a sequential workflow for beginners.

First we need to install and Configure Workflow Manger. You can install it using Web Platform Installer
image
You can find complete guide here (Prabath’s Blog).
image
image
image 
image
select the options that you need to run the workflow
  • Manual
  • Item Created
  • Item Changed
As a sample WF I’m going to get the “Age” Column in the item and Write it to log and update a column using a message.
image
Since we need to look the Age Column, i added the LookUpSPListItem to the sequence.  In theLookUpSPListItem you need to set properties to currentitem and currentlist as well as valuable you need to retrieve properties (You can change these on right side property window).
Then by clicking get properties VS generates a GetDynamicValueProperties action. In there you need to select the variable as the Source and specify properties you need to retrieve.
 image
Then drag and drop a WriteToHistory Action to the sequence. here you can go to properties and specify the message you need to log to history.
Then you can insert UpdateListItem with the Property and variable you want.
And ultimately you can deploy the workflow to your server.
image

Content Type Hub in SharePoint .

Content Type Hub
Content Type Hub is a centralized location where we manage and publish content types to other web applications.
  • Content Type Hub is actually a Site Collection.

  • Content Type is published and subscribed using the Managed Metadata Service application.
Procedure
  1. Follow the procedure below to create a content type hub. Open Central Administration then select Manage Web Applications > Create New Web Application. This will be our publishing application.
    create new web application
  2. Create a new site collection under the new web application.
    create site collection
  3. The site collection looks like the following.
    site collection looks
  4. Create a new content type under the site collection. Name it Travel Request.
    Travel Request
  5. Open Central Administration then select Manage Service Application > Choose Managed Metadata Service > Click Properties.
    Managed Metadata Service
  6. Enter the Content Type Hub URL as shown below.
    Content Type Hub URL
  7. Now choose the connection (sub item) of Managed Metadata Service.
    sub item of Managed Metadata Service
  8. Check the following options that ensure subscribing published content types.
    Publish option

  9. Back to our Source Content Type. Choose the Content Type Settings > Manage Publishing for this Content Type.
    published content types
  10. Choose the Publish option and click the OK button.
    Choose the Publish option

  11. Go to Central Administration then select Monitoring > Review Job Definitions. Select the following jobs:

    • Content Type Hub
    • Content Type Subscriber (For Default SharePoint Site)
    Content Type Hub
  12. Choose the Run Now option for each job as shown below inducing a few minutes delay in between.
    edit timer job
  13. Refresh the Subscribing site collection and you should be able to see the new content type.
    site setting
This concludes our content type publishing and subscribing.

Summary of Activities
The following is a summary of activities:
  1. Create Content Type Hub: Web Application and Site Collection
  2. Create Content Type under Site Collection
  3. Manage Metadata Service Application
  4. Manage Metadata Service Application Connection
  5. Publish Content Type
  6. Run Timer Jobs
  7. Refresh Subscribing Site Collection
  8. Ensure Content Type is visible