Sunday, 3 March 2013

Workflow Activity – Part 2

Now that we have a workflow activity defined, the next step is to add a new project to Visual Studio.
Create SharePoint Project


Next, add a project reference to the activity project.
Activity Project Reference
Now to create the workflow. Add a new item to the project and select a Sequential Workflow.
New Sequential Workflow
Give the workflow a name and set it as a list type.
New List Workflow
Next, select the option for associating with libraries and lists. You can of course opt to not auto associate and this is something I’d generally do as it allows you to control where and what the workflow is associated to when activating the feature, but that’s a different topic altogether.
While the new workflow is opened, hover over the toolbox pane and you should see the custom activity that was created in the first part of this post.
Activity Toolbox
Before adding the activity, add the ApplyActivation activity to the designer.
Apply Activation Activity
To configure the apply activation activity, we first need to create and associate a workflow context property. To do this, select the activity in the designer, bring up the properties pane and edit the __Context property.
Edit Context Property
Switch to the Bind to a new member tab and create a new field.
The difference between a property and a field is that the former creates a dependency property while the latter is a simple .NET property with no such binding.
Do the same for the _WorkflowProperties property, only this time instead of binding to a new member, select the workflowProperties item that already exists.
Bind Workflow Properties
Next, add the custom activity to the workflow, immediately after the ApplyActivation activity.
Custom Activity
Switch to the code behind for the workflow and add the following property.
public SPItemKey ListItemId
{
    get
    {
        return new SPItemKey(workflowProperties.ItemId);
    }
}
This will be used to convert the item id for the workflow to an SPItemKey object.
Head back to the designer window and bring up the properties pane for the custom activity. Here you want to bind __Context to applyActivation1_Context1 (or whatever you called the context field property). You should also bind the __ListId property to the ListId child property of applyActivation1_Context1.
Associate List Id Property
Next, you should now associate the __ItemId property with the ListItemId property, which we created via code behind.
Property Assignments
Lastly, you should enter a valid email address for the EmailTo property.
Specify Email Address
If this were a workflow for a live system this would definitely not be the way to go, you’d want to dynamically set the value here either from the parent workflow or by retrieving the value from one of the workflow item’s fields.
We’re now finished with the workflow. Next, open the feature editor – as this is a new project and we only added one workflow there should only be one feature present. To have SharePoint automatically register this workflow change the receiver assembly to Microsoft.Office.Workflow.Feature, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c and the receiver class to Microsoft.Office.Workflow.Feature.WorkflowFeatureReceiver.
Feature Receiver
Before we deploy the solution, there is one last thing we need to do and that’s ensure the assembly for the workflow activity is deployed along with the SharePoint solution. To do this open the package editor and click on the Advanced link.
Advanced Package
Click the Add button and then Add assembly from Project Output….
From the popup, select the workflow activity project and ensure the deployment target is set to the GAC.
Add Project Assembly
Deploy the solution, activate the feature and bob’s your uncle. You should now be able to associate the workflow with any list or library and receive an email after creating a new item. Ensure that your server has an outgoing email server configured and the email address set in the workflow is valid, otherwise you will get errors.

No comments:

Post a Comment