Sunday, January 25, 2009

Workflow Auto cleanup days fix

This is not new, most of us who have written atlease one Workflow in sharepoint knows this. Workflow History List is a the place where we log the progress/ comments during the Workflow progress and because of performance issues reason this list is cleaned every 60 days. But your client may not be very happy about this as they usually want to retain the history for X,Y,Z reasons.

Fortunately SPWorkflowAssociation class exposes AutoCleanupDays property and the clean up job checks the values ( in days ) before deleting the entry from the Workflow List.

Here's a small utility which can help you fix the values of AutoCleanupDays.


Code Snippet
* Date: September 17, 2007
** Program Description:
* ====================
* This program is a workaround for Microsoft Office SharePoint Server 2007
* bug #19849, where the AutoCleanupDays is set to 60 by default and by design
* in MOSS installations. This program gives the customer the oppotunity to
* change this number.
* Workflow histories would not show after 60 days by default.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace ProjectName{

class Program{

static string siteName;
static int newCleanupDays, assoCounter;
static string libraryName, wfAssoName;
static SPSite wfSite;
static SPWeb wfWeb;
static SPList wfList;

static void Main(string[] args){


switch (args.Length){

case 0:{ //no parameters entered by user
System.Console.WriteLine("Error: No arguments entered (site, library, workflow and days)");

case 4:{ //correct number of parameters
siteName = args[0];
libraryName = args[1];
wfAssoName = args[2];
newCleanupDays = Convert.ToInt32(args[3]);
assoCounter = 0;
wfSite = new SPSite(siteName);
wfWeb = wfSite.OpenWeb();
wfList = wfWeb.Lists[libraryName];
SPWorkflowAssociation _wfAssociation = null;

foreach (SPWorkflowAssociation a in wfList.WorkflowAssociations){

if (a.Name == wfAssoName){
a.AutoCleanupDays = newCleanupDays;
_wfAssociation = a;
_wfAssociation = a;


System.Console.WriteLine("\n" + wfAssoName + ": " + assoCounter.ToString() + " workflow association(s) changed successfuly!\n");

default: {//default number of parameters

System.Console.WriteLine("Incorrect number of arguments entered (" + args.Length.ToString() + " arguments)");
catch (Exception e){
System.Console.WriteLine("An error has occurred. Details:\n" + e.ToString());
finally {
if (wfSite != null)

if (wfWeb != null)

System.Console.WriteLine("\nFinished setting AutoCleanupDays!");

static void showHelpUsage() //help screen
System.Console.WriteLine("\n\nMOSS Workflow Set AutoCleanup Usage:");
System.Console.WriteLine("ShowWFs siteURL library workflow days");
System.Console.WriteLine(" - siteURL (e.g. http://serverURL/site)");
System.Console.WriteLine(" - library (e.g. \"Shared Documents\")");
System.Console.WriteLine(" - workflow (e.g. \"Approval\")");
System.Console.WriteLine(" - days for auto clean up (e.g. 120)");

Thanks to Shola Shaloko for the Code



Cole,  March 20, 2009 at 2:52 PM  

This is great but how do I get this 'utility' to run?

Sandeep K Nahta March 20, 2009 at 3:06 PM  

Create a Visual Studio Project using template for Console Application and paste the code in the Program.cs file

and Hit F5