Active Directory Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail
How Long For Me
<< Back To All Blogs
Starting SharePoint workflow for each item in a list
Thursday, October 14th, 2010
I recently came across a scenario in which we created a new workflow for a list that had over 5,000 items, and as a result of having to start the new workflow for each list item I didn't want to manually have to click all the workflow initiations, so I went to the trusty ol' console.
The console app is .NET 3.5-based and will work as follows:
1) User provides the site, list name, and workflow name in the App.config
2) The console app will open the site, then the web, then the list and iterate each item
4) The app will determine the proper Workflow Association based on the name of the workflow provided in the app config
3) It will proactively check to ensure that the workflow has not already started for each item, and if it hasn't, will start the workflow, otherwise it will skip it.
Because this is a console app you get output for each item to let you know what it's doing.
Without further ado, here is the code:
static void Main(string args)
Console.WriteLine("Connecting to Site...");
using (SPSite site = new SPSite(ConfigurationSettings.AppSettings["Site"]))
Console.WriteLine("Connecting to Web...");
using (SPWeb web = site.OpenWeb())
SPList list = web.Lists[ConfigurationSettings.AppSettings["List"]];
SPWorkflowAssociation assocFinal = null;
Console.WriteLine("Determining if workflow exists...");
foreach (SPWorkflowAssociation assoc in list.WorkflowAssociations)
assocFinal = assoc;
if (assocFinal != null)
foreach (SPListItem item in list.Items)
Console.WriteLine("Reading " + item.Title + ":");
bool shouldStart = false;
Console.Write(" Searching for running workflows... ");
SPWorkflowCollection wfs = site.WorkflowManager.GetItemActiveWorkflows(item);
if (wfs.Count == 0)
shouldStart = true;
Console.Write(wfs.Count.ToString() + " Found");
Console.Write(" Checking for Running Association ID " + assocFinal.Id.ToString() + "... ");
bool currentlyRunning = false;
foreach (SPWorkflow wf in wfs)
currentlyRunning = true;
shouldStart = true;
Console.Write(" Starting "" + ConfigurationSettings.AppSettings["Workflow"] + "" Workflow... ");
string test = item.Title;
site.WorkflowManager.StartWorkflow(item, assocFinal, assocFinal.AssociationData);
catch (Exception ex)
Console.WriteLine("Exception: " + ex.Message);
Workflow Initiatin' Tom Out.
Fixing MOSS Search Crawler issue for "The specified address was excluded..."
Fixing "The given key was not present in the dictionary" error in SharePoint 2010 Installation
Deploying Reporting Services WebParts to SharePoint WSS and MOSS
Fixing MOSS/WSS SharePoint errors with Alternate Access Mappings
Fixing SharePoint error Log with "Preserving template record"
Currently no comments.
Add A Comment
Email Address: (not public, used to send notifications on further comments)
Enter the text above, except for the 1st and last character: