Friday, December 2, 2011

How to programmatically associate a workflow to a list in Sharepoint

In one of your project we have to attach "Sharepoint approval workflow" to list when feature is activated on the web.

Prerequisite
:
  • SharePoint 2007 Workflows activated on site collection level
  • Task list of content type Task, for storing task created to users.
  private void SetWorkFlow(SPWeb web, string listName)
        {
web.AllowUnsafeUpdates = true;

string workflowAssocName = string.Format("{0} - Sharepoint Approval", listName);
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName("Approval - SharePoint 2010",
System.Globalization.CultureInfo.CurrentCulture);
SPList myList = web.Lists[listName];


// Try to get workflow history and task list
SPList historyList = web.Lists["Workflow History"];
SPList taskList = web.Lists["Tasks"];


// Create workflow association
SPWorkflowAssociation workflowAssociation = SPWorkflowAssociation.CreateListAssociation(
workflowTemplate, workflowAssocName, taskList, historyList);

// Set workflow parameters
workflowAssociation.AllowManual = false;
workflowAssociation.AutoStartCreate = true;
workflowAssociation.AutoStartChange = false;
workflowAssociation.ContentTypePushDown = false;

// The AssociationData property represents for the settings that user see in 2nd screen when creating workflow in SharePoint UI
var associationDataXml = XElement.Parse(workflowAssociation.AssociationData);

// Update modifed xml segment back to WorkflowAssociation object
workflowAssociation.AssociationData = Add_Association_Data(web.ParentWeb, associationDataXml);

// Add workflow association to my list
myList.WorkflowAssociations.Add(workflowAssociation);

// Enable workflow
workflowAssociation.Enabled = true;
myList.Update();

web.Update();
web.AllowUnsafeUpdates = false;
}

   private String Add_Association_Data(SPWeb oWeb, XElement associationDataXml)
        {
            String sDate = String.Empty;
            XNamespace dNamespace = @"http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields";
            XNamespace pcNamespace = @"http://schemas.microsoft.com/office/infopath/2007/PartnerControls";
            XNamespace dfsNamespace = @"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution";
            XNamespace MyNamespace = @"http://schemas.microsoft.com/office/infopath/2003/myXSD";
            XNamespace XsiNamespace = @"http://www.w3.org/2001/XMLSchema-instance";

            XElement propertiesWrapper = associationDataXml.Element(dfsNamespace + "dataFields").Element(dNamespace + "SharePointListItem_RW");
            propertiesWrapper.SetElementValue(dNamespace + WORKFLOW_SETTINGS_CANCELONREJECTION, true);
            propertiesWrapper.SetElementValue(dNamespace + WORKFLOW_SETTINGS_CANCELONCHANGE, true);
            propertiesWrapper.SetElementValue(dNamespace + WORKFLOW_SETTINGS_APPROVEWHENCOMPLETE, true);
            propertiesWrapper.SetElementValue(dNamespace + WORKFLOW_SETTINGS_ENABLECONTENTAPPROVAL, true);
            propertiesWrapper.SetElementValue(dNamespace + WORKFLOW_SETTINGS_EXPANDGROUPS, false);

            // Reviewers is Approvers in UI
            XElement xAssignement
                = associationDataXml.Element(dfsNamespace + "dataFields").Element(dNamespace + "SharePointListItem_RW").
                Element(dNamespace + "Approvers").Element(dNamespace + "Assignment");

            string ApprovalGroup = "Managers";
            xAssignement.Element(dNamespace + "Assignee").Add(
                new XElement(pcNamespace + "Person",
                      new XElement(pcNamespace + "DisplayName", ApprovalGroup),
                      new XElement(pcNamespace + "AccountId", ApprovalGroup),
                      new XElement(pcNamespace + "AccountType", "SharePointGroup")
                )
            );

            return associationDataXml.ToString();
        }




Hope this will work for you also.............


No comments:

Post a Comment