How to add, remove and start workflow programmatically?

Removing Association

 

    1: SPSite site = new SPSite("https://localhost");
    2: SPWeb web = site.OpenWeb();
    3: SPList list = web.Lists["Shared Documents"];
    4: SPWorkflowAssociation _association = null;
    5: foreach (SPWorkflowAssociation association in list.WorkflowAssociations)
    6: {
    7: if (association.Name == "Approval")
    8: {
    9: _association = association;
   10: break; 
   11: }
   12: }
   13: if(_association !=null)
   14: list.RemoveWorkflowAssociation(_association);

 

Adding workflow to the library

    1: SPSite site = new SPSite("https://localhost");
    2: SPWeb web = site.OpenWeb();
    3: SPList list = web.Lists["Shared Documents"];
    4: SPList taskList = web.Lists["Tasks"];
    5: SPList historyList = web.Lists["Workflow History"]; 
    6: SPWorkflowTemplate _template = null;
    7: foreach (SPWorkflowTemplate template in web.WorkflowTemplates)
    8: {
    9: if (template.Name == "Approval")
   10: {
   11: _template = template;
   12: break; 
   13: } 
   14: }
   15: SPWorkflowAssociation asso = SPWorkflowAssociation.CreateListAssociation(_template, _template.Name, taskList, historyList); 
   16: list.AddWorkflowAssociation(asso);

Starting workflow

 

    1: SPSite site = new SPSite("https://localhost");
    2: SPWeb web = site.OpenWeb();
    3: SPList list = web.Lists["Shared Documents"];
    4: SPWorkflowAssociation association = AddWorkflow(); // Above function to get the SPWorlflowAssociation object
    5: SPWorkflowManager manager = site.WorkflowManager; 
    6: for (int idx = 0; idx < 10 ; idx++) // I'm assuming there would be atleast 10 items within the library
    7: {
    8: SPListItem item = list.Items[idx];
    9: manager.StartWorkflow(item,association, "you can pass any data here",true);
   10: }

Comments

  • Anonymous
    August 27, 2008
    PingBack from http://hubsfunnywallpaper.cn/?p=2679

  • Anonymous
    November 23, 2008
    Hi, Im trying to remove a specific workflow from all pages list in a sitecollection. but i get this error when i run though the site: Collection was modified; enumeration operation may not execute.   at Microsoft.SharePoint.SPBaseCollection.SPEnumerator.System.Collections.IEnumerator.MoveNext() in this line of code: "foreach (SPList currentList in currentWeb.Lists)" This is the code i use:            using (SPSite site = new SPSite(SPContextID))            {                foreach (SPWeb currentWeb in site.AllWebs)                {                    try                    {                        foreach (SPList currentList in currentWeb.Lists)                        {                            if (currentList.Title.ToLower().Equals("pages"))                            {                                currentList.EnableModeration = true;                                foreach (Microsoft.SharePoint.Workflow.SPWorkflowAssociation wfa in currentList.WorkflowAssociations)                                {                                    if (wfa.Name.Equals("Parallel Approval"))                                    {                                        currentList.RemoveWorkflowAssociation(wfa);                                        break; // Only this workflow has to be removed, bail out when done.                                    }                                }                            }                        }                    }                    finally                    {                        currentWeb.Dispose();                    }                }            } Do you have any idea's?

  • Anonymous
    January 03, 2010
    @Peter: RemoveWorkflowAssociation called in a foreach will modify the iterator as it goes... use a for or while loop instead -- Michhes

  • Anonymous
    March 15, 2012
    Is it possible to change the approvers dynamically.???

  • Anonymous
    February 05, 2015
    How to remove workflow from SPlistItems??