Hi, since long i was thinking to blog about this topic and finally got a chance to do so. Normally we prefer to create Designer Based Workflows, as they are easy to create, manage and quite flexible.
But still at situations Designer Workflows not allow us to do certain Tasks, for an instance , performing cross site Tasks, like Creating a Document Library in a different site or Creating a new site on certain conditions.
Instead of Creating Complete Workflow we can still use Custom Activities Development in Visual Studio 2010 which is much easier to built and easy to manage for client's Power Users.
So what i mean is simple “Code an Activity” instead of Coding a Complete Workflow.
Now lets come to the point and follow some steps to create a simple Workflow Activity. Remember this will be Number 1, and quite easy one, later i will post some bigger and much cruciall Activities.
What I’m about to show you is what I did and tested in the following environment:
• Microsoft Windows server 2008 R2 Enterprise Edition
• SharePoint Server 2010 Enterprise Edition
• MS SQL 2008 Enterprise Edition
• Visual Studio 2010 Ultimate
• Code was done in C# only
(Note: we don’t need the exact environment, but as such a similar one to get started)
Step: 1) Create a Project to support Activity Deployment to Farm
Create an Empty Project in VS2010 and Name it “CustomActivityDemo”, make Sure it should be a Farm Scoped Project.(No SandBoxed), And select the site on which you like to test it /*Empty SharePoint Project*/
Next Click Finish.. It will be just like a scenerio where this project will act as a Rocket Launcher for our Rocket(Activity).. :)
We will setup code and properties later in this, now lets move ahead and create our Activity Project in the Same Solution.
Step :2) Create WorkFlow Activity Project
At the Solution level Right Click and “Add a Project” in VS2010.
Next Select the Visual C# >Workflow >Workflow Activity Library and set the project name as ‘CreateSiteActivity’.
Now whole solution will appear like this:
Note that after adding Workflow Activity Library Project a Design view of Activity appears which shows Activity1.cs
lets now follow these subpoints:
• Right click the Activity1.cs and rename it to “CreateSiteActivity.cs”,
• Again Right Click “CreateSiteActivity” and click “View Code”.
• Now add a reference to
Microsoft.SharePoint.dll and Microsoft.SharePoint.WorkflowActions.dll located in ISAPI Library:
• Import the following NameSpaces in the code file:
This will how the code will appear now:
Before going further lets discuss some of the important terminologies:
Step :3) Some Introduction to WorkFlow Terminologies:
This will be boring a bit but very quickly please do read these lines as it will clear our process in a bigger way:
Dependency Property
Dependency properties provide a centralized repository of a workflow's state.
The
DependencyObject is a hash table that stores the current values of any DependencyProperty that is applied to it.
Dependency Object Class:
An instance type of a dependency property can be bound to instance data, in which case the actual value is not determined until runtime.
You set this type of dependency property's value to be an ActivityBind to bind to the actual value that is accessed at runtime.
ActivityBind binds an activity property to any of the following:
• Another activity property.
• A field.
• A property.
• A method.
In Simpler Word:
Dependency Property provide a mechanism that allows SharePoint Designer to provide values for your custom action before you add your custom functionality.
To allow SharePoint Designer to pass values to your custom action when the workflow runs, you must create public properties inside your custom action. In this example, you will create a custom action that will create a new site within SharePoint whenever executed inside a SharePoint Designer workflow.
Before Writing any Dependency Property and its Binding Property, we have to think about our Inputs, so in this example here are the inputs:
SiteUrl: Where the site will be located.
SiteTitle: Title for the new site.
SiteDescription: Description for the new site.
SiteTemplate: Template to use to create the new site.
InheritPermissions: True, if the new site will inherit permissions from its parent site.
For each field, you create a property in your custom action to allow the users to specify these values when designing their workflow in SharePoint Designer.
When creating properties for a custom action, you create the property itself and a matching DependencyProperty. The DependencyProperty class allows SharePoint Designer to bind the property to the UI in the workflow wizard, where the user specifies the values of these properties in the workflow being created.
Note: Please follow the Syntax we will use while Creating a Property and a Matching Dependency Property, there is Matching Relation require in both of these Names.
Add the following code to create a property and DependencyProperty for SiteUrl:
Parameters it takes are : 1)String Name of the associated Property, Type of Property, Type of Owner(Activity Class Name)
Now for a while just think on what we have written:
Dependency Property Name : SiteUrlProperty and Matching Property Name is SiteUrl
It is required to follow above Syntax i.e always write Dependency Property with Suffix “Property” if you miss this then you will get Compilation Error.
Step: 4) Getting Current Workflow Context