Wednesday, 2 May 2012

SharePoint 2007: Workflow History lost?

Recently I had a customer complaining that after a period of time their approval workflows history was disappearing. By Default SharePoint removes any workflow history and tasks after 60 days period.

So I created a script that increases the AutoCleanupDays of the SPWorkflowAssociation through the whole site collection (given as paramenter for the command script). The code sample below increases all Worflow associations expiration to 99999 days, which is more than enough to keep track of your documents history approval.

If you wish you can recycle the code as a timer job.

Code sample:
using (SPSite sitecollection = new SPSite(http://mossdev))
{
using(SPWeb site = sitecollection.OpenWeb()){
   SPWorkflowAssociation _assoc = null;
   SPList list = site.Lists["Shared documents"];
  foreach (SPWorkflowAssociation assoc in  list.WorkflowAssociations)
   {
        _assoc = assoc;
        _assoc.AutoCleanupDays = 99999;                        
   }
   list.UpdateWorkflowAssociation(_assoc);
   list.Update();
   }                
 }

This code increases the expiration period of a workflow history to 99999 days, 
for all Workflow associations in the opened web object.
It saves an HTML file in the root Documents library with a log of the changes 
performed, with a date stamp.

Script & Code source files:
WorkFlow_AutoCleanUpConfigScript.zip (3.45 KB)
WorkFlow_AutoCleanUpConfigSource.zip (802.16 KB)

No comments: