Quantcast
Channel: list – farhanfaiz's Weblog – SharePoint 2003/07/10/13/16/19 & Online
Viewing all articles
Browse latest Browse all 9

SharePoint: Associate Workflow with a list programmatically

$
0
0

Scenario:

We need to associate a workflow with a list or create an instance of workflow for a list using SharePoint Object Model (OM)

Resolution:

In order to associate workflow with a list, we will be using CreateListAssociation method of SPWorkflowAssociaition class. We need following four values:

  • baseTemplate – The workflow template on which to base this workflow association.
  • Name – The name to give this workflow association.
  • taskList – The task list on which to create workflow tasks for this workflow association.
  • historyList – The list to which to log workflow history events

Code is as under:

using(SPSite spSite = new SPSite(“SiteURL”))

{

SPWeb spWeb = spSite.OpenWeb();

SPList assocList = spWeb.Lists["NameOfTheList"];

SPWorkflowTemplate wflTemplate = spWeb.WorkflowTemplates.GetTemplateByName(“NameOfWorkflowTemplate”, System.Globalization.CultureInfo.CurrentCulture);

SPList wflTaskList = spWeb.Lists["Tasks"];

SPList wflHistoryList = spWeb.Lists["Workflow History"];

SPWorkflowAssociation wflAss = SPWorkflowAssociation.CreateListAssociation(wflTemplate, “NameOfTheWorkflow”, wflTaskList, wflHistoryList);

wflAss.AutoStartChange = false;

wflAss.AutoStartCreate = false;

wflAss.AllowManual = true;

spWeb.AllowUnsafeUpdates = true;

assocList.AddWorkflowAssociation(wflAss);

assocList.Update();

spWeb.AllowUnsafeUpdates = false;

}



Viewing all articles
Browse latest Browse all 9

Trending Articles