another technical blog...technically

Sunday, June 22, 2014

Visual studio SharePoint 2013 workflow walktrough

In this article i will show a brief (hopefully complete) example about workflow development in SharePoint 2013. If you have developed sharepoint workflows in visual studio, you could find this article useful.

Before developing any workflow, it's necessary to link SharePoint web application to the workflow web application, so they can talk to each other.
Register-SPWorkflowService -SPSite <SharePoint Site Url> -WorkflowHostUri <Workflow web application> -AllowOAuthHttp -Force
The code of the example below can be downloaded here (or below) and it represents the first step for my little DLL for helping SharePoint development (Applause).
Please refers to the Example 1 feature referenced code to follow the example below.
In this walktrough i will develop a simple approval workflow with dinamically chosen approvers, so we assume the list item contains a SPUser multivalue field named "Approvers" (Yep, very original) with this information.
And now we can walk trough the code.

Let's have a look on the first activity, LookupSPListItem, which is one of the most important.
Like in the past, we have to bind it to the ItemId and the ListId.

Clicking on GetProperties creates a new activity below, named GetDynamicValueProperties.
Clicking on ViewProperties we can choose the entity type (in this case List Item of Item2Approve), point to the Path and assign the return value to a worflow scoped variable.
Clicking on Variables we can see all workflow scoped variables, you can notice we have approvers user in a Collection<string> variable users (remember that no SPUser variable could be a workflow scoped one because it is not a serializable class).
 
And now the most simple part, with composite task we can configure parallel/sequence task simply assigning the variable in the AssignedTo field. CompositeTask activity will resolve automagically SPUser for us.
 
And yeah, it works like a charm.
Now, let's think about how to start the workflow programmatically. In SharePoint 2010 you can find the workflow association of the list using WorkflowAssociations property of SPList object. In SP2013 I spent a lot of time trying to figure out why this property was not populated with my workflow, when i found out also the workflow template was not registered to the site.
So i realize...

Remember the stuffs about the other web application and so on?
We have to call the new shiny Workflow service manager.
At first we havo to include the (almost hidden) library
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.WorkflowServicesBase
Then we have to call this piece of code
public static void StartWorkflow(SPListItem item, Guid workflowDefinitionId, Dictionary parameters)
{
    SPSecurity.RunWithElevatedPrivileges(() =>
    {
        if (parameters == null)
        {
            parameters = new Dictionary();
            parameters.Add("a property", "a value");
        }

        SPWeb web = item.Web;
        SPList list = item.ParentList;

        WorkflowServicesManager workflowServiceManager = new WorkflowServicesManager(web);
        WorkflowSubscriptionService workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();
        WorkflowSubscriptionCollection subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(list.ID);

        WorkflowSubscription subscription = subscriptions.FirstOrDefault(w => w.DefinitionId.Equals(workflowDefinitionId));

        if (subscription != null)
        {
            workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(subscription, item.ID, parameters);
        }
    });
}
And yep, for now, that's all. May the force be with you.

Download source code

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?