Pages

Monday, May 27, 2013

Programmatically Start SharePoint 2013 Workflow

SharePoint 2013 introduced a new set of API to work with new workflow model. In my previous post I described how to access workflows associated with a list/list item and access workflow properties. In this post I’ll describe how to run SharePoint 2013 workflow programmatically.

First you need to create instance of WorkflowServicesManager, then you need to find out the subscription (which actually represent the workflow association). Finally you can run the workflow with WorkflowInstance Service as shown below:

var workflowServiceManager = new WorkflowServicesManager(web);
var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();

//get all workflows associated with the list
var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(listId);

//run all workflows associated with the list
foreach (var workflowSubscription in subscriptions)
{
    //initiation parameters
    var inputParameters = new Dictionary<string, object>();
    inputParameters.Add("MyProperty", "MyValue");

    workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, itemId, inputParameters);
}

In the StartWorkflowOnListItem, if you have any input parameters (which you usually pass from workflow initiation form), you can pass the input paramters as a dictionary. Otherwise, you can pass an empty dictionary.

 

Similarly you can terminate or suspend workflows using Workflow Instance Service.

For your information, workflow will not run if you use an elevated web in WorkflowServicesManager. You need to make sure you are running the code with an user who is not system account and who has a user profile.

Saturday, May 25, 2013

SharePoint 2013 Workflow: Programmatically Access Workflow Properties

SharePoint 2013 workflow introduced a new API to interact with SharePoint 2013 workflows. The old workflow API will not work for this new workflow model. The most basic API is available as part of the DLL (Microsoft.SharePoint.WorkflowServicesBase.dll) and the basic entry point for accessing workflow data is WorkflowServicesManager class. I’ll explain few uses of API but you can browse the API to find out more details usage.

 

Get all Workflows associated with a list

You need to create a new instance of WorkflowServiceManager and using the list id, you can ask WorkflowSubscriptionService to find all workflows associated with the list as shown below:

WorkflowServicesManager workflowServiceManager = new WorkflowServicesManager(web);
var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();
var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(listId);

You can get subscription properties as shown below. If you association form has any custom properties, you can retrieve these property values by passing property name.

foreach (var subscription in subscriptions)
{
    var assocationName = subscription.Name;
    var propertyValue = subscription.GetProperty("Property_Name");
}

 

Get Workflow Instance

To get workflow instance details we need get the instance of WorkflowInstanceService from WorkflowServiceManager class as shown below:

WorkflowServicesManager workflowServiceManager = new WorkflowServicesManager(web);
var workflowInstanceService = workflowServiceManager.GetWorkflowInstanceService();
var workflowInstances = workflowInstanceService.EnumerateInstancesForListItem(listId, itemId);

Once we get the workflow Instance, we can get the instance properties as shown below. Any properties in Workflow Initiation form will also be available:

foreach (var instance in workflowInstances)
{
    var propertyValue= instance.Properties["Property_Name"];
}

The table below shows few common properties, but you can find out more properties by debugging code:

Property Name

Description

Microsoft.SharePoint.ActivationProperties.ContextListId

List Id

Microsoft.SharePoint.ActivationProperties.CurrentItemUrl

Current Item Url

Microsoft.SharePoint.ActivationProperties.InitiatorUserId

Initiator User login name, for example i:0#.w|domain\username

Microsoft.SharePoint.ActivationProperties.ItemId

List Item Id

 

Send Custom Notification to Workflow

In SharePoint 2013 workflow, there’s a activity ‘WaitForCustomEvent’. Using this activity you can keep your workflow wait for a custom event. In this activity you can pass the event name. To send the custom event to the workflow you can use workflow API as shown below. First you need to find out the workflow Instance.

WorkflowServicesManager workflowServiceManager = new WorkflowServicesManager(web);
var workflowInstanceService = workflowServiceManager.GetWorkflowInstanceService();
workflowInstanceService.PublishCustomEvent(workflowInstance, "CustomEventName", "");