NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
How Long For Me
SaveMySerials

Blog
Twitter

NerdyHearn - Blog


<< 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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace WorkflowStarter
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Connecting to Site...");
using (SPSite site = new SPSite(ConfigurationSettings.AppSettings["Site"]))
{
Console.WriteLine("Connecting to Web...");
using (SPWeb web = site.OpenWeb())
{
Console.WriteLine("Obtaining list...");
SPList list = web.Lists[ConfigurationSettings.AppSettings["List"]];

SPWorkflowAssociation assocFinal = null;

Console.WriteLine("Determining if workflow exists...");
foreach (SPWorkflowAssociation assoc in list.WorkflowAssociations)
{
if (assoc.Name.Equals(ConfigurationSettings.AppSettings["Workflow"]))
{
assocFinal = assoc;
Console.WriteLine("Workflow found");
}
}

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)
{
Console.Write("0 Found");
shouldStart = true;
}
else
{
Console.Write(wfs.Count.ToString() + " Found");
Console.WriteLine();
Console.Write(" Checking for Running Association ID " + assocFinal.Id.ToString() + "... ");
bool currentlyRunning = false;
foreach (SPWorkflow wf in wfs)
{
if (wf.AssociationId.Equals(assocFinal.Id))
{
Console.Write("Found");
currentlyRunning = true;
}
}
if (!currentlyRunning)
{
Console.Write("Not Found");
shouldStart = true;
}
}
Console.WriteLine();

if (shouldStart)
{
Console.Write(" Starting "" + ConfigurationSettings.AppSettings["Workflow"] + "" Workflow... ");
string test = item.Title;
site.WorkflowManager.StartWorkflow(item, assocFinal, assocFinal.AssociationData);
Console.Write("Done");
}

Console.WriteLine();
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
}
}


Workflow Initiatin' Tom Out.

Tags

SharePoint

Related Blogs

Adding a Custom ASP.NET Page to MOSS/WSS
Fixing 400 RequestLength Issue in SharePoint and IIS
Adding jQuery to SharePoint
Determining if MOSS is Standard or Enterprise

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers