Step by Step Tutorial : Creating workflows for SharePoint 2010: Creating custom activities (step 3/15)

image_thumb

 

SharePoint 2010 workflow tutorial

 

  1. Step 1.
  2. Step 2.
  3. Step 3.
  4. Step 4.
  5. Step 5.
  6. Step 6.
  7. Step 7.
  8. Step 8.
  9. Step 9
  10. Step 10.
  11. Step 11.
  12. Step 12
  13. Step 13.
  14. Step 14.
  15. Step 15.

 

SharePoint 2007 workflow tutorial

 

  1. Step 1.
  2. Step 2.
  3. Step 3.
  4. Step 4.
  5. Step 5.
  6. Step 6.
  7. Step 7.
  8. Step 8.
  9. Step 9.
  10. Step 10.
  11. Step 11.
  12. Step 12.
  13. Step 13.
  14. Step 14.
  15. Step 15.
  16. Step 16.
  17. Step 17.

 

Introduction

In this tutorial I will illustrate how to create custom activities in a SharePoint 2010 code workflow.To achieve it, I will extend the workflow we created in step 1 and step 2. Make sure you have the correct site template as well as the code generated in step 2.

The scenario

Our expense report approval workflow will automatically approve an expense report if the amount is smaller than 1000 otherwise the expense report will have to be approved by the manager of the person who submitted the report.

The person who submitted the report can easily be retrieved from the (default) column Created By   :

image

[Figure 1]

However if you open the list (and fields) definition with a (free) tool like SharePoint 2010 Manager (or Server Explorer in Visual Studio 2010), you will notice that the internal name of this column is “Author

image

[Figure 2]

Our Managers list is designed like this :

image

[Figure 3]

So if the user who submits an expense report is Brianc Cox, we want to be able to retrieve his manager: Amy Alberts.

If we debug our previous code, we will notice that the Author property return the following string

image

[Figure 4]

Testing the (custom) FindManager function

A generic function “FindManager” capable of retrieving a user account from this kind of string is provided in the script1.txt file.

This function can be invoked like this :

image

[Figure 5]

Copy & Paste the FindManager function from snippet1.txt and invoke it from the requestManagerApproval event handler

image

[Figure 6]

image

[Figure 7]

Run your workflow on a list item that requires manager approval and you get this :

image

[Figure 8]

(In my case I logged in as Brianc Cox to fill in the expense reports information, and the associated manager, Amy Alberts (CONTOSO\amya) has been retrieved by the FindManager function.

Custom activity

If we want to reuse  the FindManager function in several workflows, one option is to encapsulate it in an Custom activity.

Add a new Workflow project the  solution : select the Workflow  Activity Library template; name the project LitwareActivities:

 image

[Figure 9]

In this project, reference  Microsoft.Sharepoint.dll in order to use the Sharepoint Object Model.

Delete the file Activity1.cs and add a new Activity component (Select Add new Item-Activity).

Name the file RetrieveManager.cs.

To be able to pass the parameter required for the FindManager function from the workflow to the activity, we need to define dependency properties in the activity class. In workflow foundation (3.0 and 3.5) this the common communication pattern between a workflow and custom activities or even between activities in the same workflow.

Go to the code behind and declare a using Microsoft.SharePoint namespace statement.

Define a dependency property for each property (see next step how to achieve it) of the custom activity that can only be retrieved at runtime :

  • WebSite
  • Managee
  • Manager
  • ManageeColumName
  • ManagerColumnName
  • ManagerListTitle The define dependency properties go to the activity code code behind and add the following snippet :image

[Figure 10]

Create a property named  WebSite  : click on tab key and specify SPWeb as the type instead of string; you should get something like this :

image

[Figure 11]

Follow the same procedure for :

  • Managee
  • Manager
  • ManageeColumName
  • ManagerColumnName
  • ManagerListTitle (type must be string)

    Recompile the whole solution, click on the workflow class to bring it into the workflow designer, and you will notice your new custom activity in the toolbox:

    image

    [Figure 12]

    Drag and drop it into the ifImportantAmount activity just before the code activity; remove the code activity.

    Rename retrieveManager1 as retrieveManager.

    Your workflow should look like this

    image

[Figure 13]

In our case the retrieveManager activity is a sequential activity that can host other activities; we don’t really need that : let’s go back to the custom activity codebehind and derive it from the Activity class

image

[Figure 14]

Rebuild the solution and refresh the workflow in the workflow designer

image

[Figure 15]

The retrieveManager activity look and feel has been modified.It is good to know that we can provide a custom look and feel to an activity by providing a custom Activity Designer : this is beyond the scope of this article, but you will find more information about it here.

Go back to the custom activity source code and override the Execute method as follows :

image

[Figure 16]

Move the FindManager function from the workflow code to the custom activity code and invoke it from the Execute function :

image

[Figure 17]

Sign the custom activity project by reusing the workflow project key file .

Let’s provide some parameters to the retrieveManager activity : if you select the activity in the Designer, the property page will display the ManageeColumName, ManagerColumName, ManageListTitle and WebSite.

image

[Figure 18]

Let’s provide a link between between the activity properties and the workflow by binding each activity property with a workflow property : select the ellipsis button associated with each activity, select the Bind to a new Member tab, select Create Field and provide a field name :

image

[Figure 19]

Let’s provide the Managee property by (data)binding it to the workflow public Author property : select the Managee property in the property page (at the workflow level) , click on the ellipsis (…) button, select Bind to existing member and select Author.

Follow the same procedure with the WebSite property, but bind it to the Web member of workflowProperties workflow public member as illustrated in the next picture:

image

[Figure 20]

Bind the Manager property to a new field called Manager in the workflow class.

Drag & drop a code activity after the retrieveManager activity  and name it askManager :

image

[Figure 21]

Now that the manager name has been fetched, we can change the item status as we did previously in the _ExecuteCode function :

image

[Figure 22]

The Custom activity need to be deployed alongside : go to the workflow project, select the Package Folder :

image

[Figure 23]

Double click on the Package folder, select the Advanced tab; in Additional Assemblies, click on Add Assembly from Project Output :

image

[Figure 24]

Select LitwareActivities as the source project (and make sure the Deployment Target is GlobalAssemblyCache ):

image

[Figure 25]

Click ok.Select Build and Deploy.Run the workflow on an item created by Brianc Cox (you can log in as Brian Cox), set the expense report amount to 2000 and the item status should be must be approved by Amy Alberts.

image

[Figure 26]

If you get an error, you can try to debug the workflow (F5 will do the job) or by attaching the Custom activity project (and therefore the assembly) to the w3wp.exe processes.

Final code can be downloaded from my Skydrive here.

Advertisements

3 responses to “Step by Step Tutorial : Creating workflows for SharePoint 2010: Creating custom activities (step 3/15)

  1. Is this the final step of the approval workflow? Or are there any more? If there are any more, where are the links?

  2. Your step 1 is so god But what is FindManager function in the step 2. How do we create this FindManager function. Please tell me. Because of this function I m nit able to go further. Looking forward to your response. Please its urjent.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s