Associating a workflow to a content type

We added metadata columns to our content type, we added a document template that provides the common format for uploading project proposals to the library, and in this recipe we will add "Approval workflow" to our content type.

Approval Workflow is out-of-the-box workflow available in the SharePoint server.

Getting ready

You should have completed the previous recipes successfully.

How to do it...

  1. If you have closed Visual Studio IDE, launch it as an administrator.
  2. Open the previously created ProjectProposal solution.
  3. Open Feature1.EventReceiver.cs file and add code to associate the workflow before the content type update method in the feature's activating method. Your code should look like the following:
    //Associate the document template created.
    ctProjectProposal.DocumentTemplate = "/_layouts/
    ProjectProposal/ProjectProposal.dotx";
    SPWorkflowTemplate approvalTemplate = null;
    for (int i = 0; i < web.WorkflowTemplates.Count; ++i)
    {
    if (web.WorkflowTemplates[i].Name == "Approval - SharePoint 2010")
    approvalTemplate = web.WorkflowTemplates[i];
    }
    SPList wrkHistoryList = null;
    // Try to get workflow history list
    try
    {
    wrkHistoryList = web.Lists["Workflow History"];
    }
    catch (Exception)
    {
    // Create workflow history list
    Guid listGuid = web.Lists.Add("Workflow History", "", SPListTemplateType.WorkflowHistory);
    wrkHistoryList = web.Lists[listGuid];
    wrkHistoryList.Hidden = true;
    wrkHistoryList.Update();
    }
    SPList wrkTasksList = null;
    // Try to get workflow tasks list
    try
    {
    wrkTasksList = web.Lists["Tasks"];
    }
    catch (Exception)
    {
    // Create workflow tasks list
    Guid listGuid = web.Lists.Add("Tasks", "", SPListTemplateType.Tasks);
    wrkTasksList = web.Lists[listGuid];
    wrkTasksList.Hidden = true;
    wrkTasksList.Update();
    }
    Microsoft.SharePoint.Workflow.SPWorkflowAssociation wrkAssoc = SPWorkflowAssociation.CreateListContentTypeAssociation(approvalTemplate, "Chapter3 Project Proposal Approval", wrkTasksList, wrkHistoryList);
    wrkAssoc.AutoStartCreate = false;
    ctProjectProposal.WorkflowAssociations.Add(wrkAssoc);
    // Commit changes.
    ctProjectProposal.Update();
    
  4. Build and run the project. Navigate to Site Actions | Site Settings | Gallery | Site Content Types and select Chapter3 Project Proposal. Click on the Workflow Settings and you should be able to see our "Approval workflow" associated with the content type as shown in the following screenshot:
How to do it...

How it works...

The first step to associate a workflow to a content type is to get the workflow template that we need to associate. We loop through the Web object to get our template. In our example, we got the template for "Approval SharePoint 2010". This is the name of the workflow. When you know the name of the workflow, you can iterate through the Web object for all the workflows that are available on that site. After this, we created the necessary supporting lists like the tasks and the workflow history list for the workflow. Based on the template, we created an association object for the content type and associated this object to our content type.

A workflow associated object represents the binding of the template to a particular content type or list. It has a static method to create the binding association. In our case, we used the CreateListContentTypeAssociation method to create the binding association. For more information on workflow association please refer to MSDN at: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflow.spworkflowassociation.aspx.

There's more...

You can also pass the association data to the workflow template. Association data is of the XML format and is different for each of the workflows. The following is the code to pass association data to the approval workflow that we have used. Make sure to substitute proper user IDs for your environment.

string sAssocData = "<dfs:myFields xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dfs:queryFields></dfs:queryFields><dfs:dataFields><d:SharePointListItem_RW><d:Approvers><d:Assignment><d:Assignee><pc:Person><pc:DisplayName>Balaji Kithiganahalli</pc:DisplayName><pc:AccountId>INTEGRATELLCDEV\balaji</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person><pc:Person><pc:DisplayName>SP Test1</pc:DisplayName><pc:AccountId>INTEGRATELLCDEV\sptest1</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person></d:Assignee><d:Stage xsi:nil="true" /><d:AssignmentType>Serial</d:AssignmentType></d:Assignment><d:Assignment><d:Assignee><pc:Person><pc:DisplayName>SP Test2</pc:DisplayName><pc:AccountId>INTEGRATELLCDEV\sptest2</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person></d:Assignee><d:Stage xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /><d:AssignmentType>Serial</d:AssignmentType></d:Assignment></d:Approvers><d:ExpandGroups>true</d:ExpandGroups><d:NotificationMessage /><d:DueDateforAllTasks xsi:nil="true" /><d:DurationforSerialTasks xsi:nil="true" /><d:DurationUnits>Day</d:DurationUnits><d:CC /><d:CancelonRejection>false</d:CancelonRejection><d:CancelonChange>false</d:CancelonChange><d:EnableContentApproval>false</d:EnableContentApproval></d:SharePointListItem_RW></dfs:dataFields></dfs:myFields>";
approvalTemplate.AssociationData = sAssocData;

See also

  • Creating an external content type recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.130.24