Sunday, January 25, 2009

Workflow Auto cleanup days fix

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

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

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){

try{

switch (args.Length){

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

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;
assoCounter++;
}
else{
_wfAssociation = a;
}
}

wfList.UpdateWorkflowAssociation(_wfAssociation);

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

default: {//default number of parameters

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

if (wfWeb != null)
wfWeb.Dispose();

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

static void showHelpUsage() //help screen
{
System.Console.WriteLine("\n\nMOSS Workflow Set AutoCleanup Usage:");
System.Console.WriteLine("====================================");
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)");

}
}
}
Article:
Thanks to Shola Shaloko for the Code

MSDN

2 comments:

Anonymous,  March 20, 2009 at 2:52 PM  

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

Sandeep 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