Nishand's Blog

Welcome to my personal blog, All the information in this bogs is my ideas,findings and thoughts on .Net, Asp.Net and SharePoint.
ALL POSTING ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND

How to add, remove and start workflow programmatically?

Removing Association

 

   1: SPSite site = new SPSite("http://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("http://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("http://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: }
Published Wednesday, August 27, 2008 4:08 PM by nishandv

Comments

 

send flowers &raquo; How to add, remove and start workflow programmatically? said:

August 27, 2008 7:15 PM
 

PeterRiise said:

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?

November 24, 2008 5:17 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker