Software Technology Tips

Some cases we need to associate the workflow to a list programatically.The below example show how we can attach a workflow to a list programatically.The workflow can be standard or designed with visual studio or sharepoint designer.
 

SPWorkflowTemplate workflowTemplate = null;                // Workflow template
SPWorkflowAssociation workflowAssociation = null;         //Workflow association
SPList historyList = null;                                                     // Workflow history list
SPList taskList = null;                                                        // Workflow tasks list
SPList list = null;                                                               //Sharepoint List       
 
using (SPSite oSPsite = new SPSite("http://website_url/"))
{
    oSPsite.AllowUnsafeUpdates = true;
 
    using (SPWeb oSPWeb = oSPsite.OpenWeb())
    {
 
                 web.AllowUnsafeUpdates = true;
            
                 workflowTemplate = web.WorkflowTemplates.GetTemplateByName( "Template name",  
                                                             System.Globalization.CultureInfo.CurrentCulture);
 
                  list  = oSPWeb.Lists["List Name"];     //List which we is going to associate the workflow           
                  historyList = oSPWeb.Lists["Workflow History"];  //The list to which to log workflow history events.

                  taskList = oSPWeb.Lists["Workflow Task"];      //The task list on which to create workflow tasks for this workflow
                                                                                                   association.
 
                 try
                {
                            // Create workflow association
                           workflowAssociation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate,
                                                                 workflowAssocName, taskList, historyList);
 
                         // Set workflow parameters 
                        workflowAssociation.AllowManual = false;
                        workflowAssociation.AutoStartCreate = true;
                        workflowAssociation.AutoStartChange = false;
 
                        // Add workflow association to my list
                       list.AddWorkflowAssociation(workflowAssociation);
 
                      // Enable workflow
                      workflowAssociation.Enabled = true;
              }
             finally
             {
                    web.AllowUnsafeUpdates = false;
             }
}
 


Related Tags:

SharePoint

Author: Premananda Das

Related Pages

  • SharePoint Programming Expertise
  • SharePoint Development Projects

SharePoint

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login