SharePoint Workflow History “Disappears”

By default, workflow history will "disappear" after 60 days.  Microsoft has built this into MOSS intentionally to ensure good performance.  By deleting old workflow history, it’s more likely (but hardly guaranteed) that the Workflow History list (/Lists/Workflow History by default) will stay below the magic 2000 items per list limit.  (See the "Working with Large Lists in Office SharePoint Server 2007" white paper for details on this limit.)

The official Microsoft answer to this in Technet is to disable the Workflow Auto Cleanup Timer Job.  Unfortunately, this is a farm-level setting and will affect every Site Collection in your farm, which may be more far-reaching than you intend.  However, it might be the right solution for you.

Another option is to run some code to change the time length for the history retention.  There is some good code and discussion here (I’m reproducing the code below in case the link changes.  Thanks to Shola Salako!  I haven’t tested this code, so use it carefully.) to do this by changing the value of SPWorkflowTemplate.AutoCleanupDays on specific lists.  NOTE: Don’t use the SQL method!  Running the C# example which utilizes the object model is perfectly legitimate, but poking around in the database will leave you in what Microsoft calls an "unsupported state" and is just a bad idea.

For companies that rely on workflow history for auditing and regulatory compliance, taking one of these actions is going to be critical.

   1: /*
   2: * Date: September 17, 2007
   3: * 
   4: * Program Description:
   5: * ====================
   6: * This program is a workaround for Microsoft Office SharePoint Server 2007
   7: * bug #19849, where the AutoCleanupDays is set to 60 by default and by design
   8: * in MOSS installations. This program gives the customer the opportunity to
   9: * change this number.
  10: * Workflow histories would not show after 60 days by default.
  11: */
  12: using System;
  13: using System.Collections.Generic;
  14: using System.Text;
  15: using Microsoft.SharePoint;
  16: using Microsoft.SharePoint.Workflow;
  17:  
  18: namespace ShowWFs
  19: {
  20: class Program
  21:     {
  22: static string siteName;
  23: static int newCleanupDays, assoCounter;
  24: static string libraryName, wfAssoName;
  25: static SPSite wfSite;
  26: static SPWeb wfWeb;
  27: static SPList wfList;
  28: static void Main(string[] args)
  29:         {
  30: try
  31:             {
  32: switch (args.Length)
  33:                 {
  34: case 0: //no parameters entered by user
  35:                         {
  36:                             System.Console.WriteLine("Error: No arguments entered (site, library, workflow and days)");
  37:                             showHelpUsage();
  38: break;
  39:                         }
  40: case 4: //correct number of parameters
  41:                         {
  42:                             siteName = args[0];
  43:                             libraryName = args[1];
  44:                             wfAssoName = args[2];
  45:                             newCleanupDays = Convert.ToInt32(args[3]);
  46:                             assoCounter = 0;
  47:                             wfSite = new SPSite(siteName);
  48:                             wfWeb = wfSite.OpenWeb();
  49:                             wfList = wfWeb.Lists[libraryName];
  50:                             SPWorkflowAssociation _wfAssociation = null;
  51: foreach (SPWorkflowAssociation a in wfList.WorkflowAssociations)
  52:                             {
  53: if (a.Name == wfAssoName)
  54:                                 {
  55:                                     a.AutoCleanupDays = newCleanupDays;
  56:                                     _wfAssociation = a;
  57:                                     assoCounter++;
  58:                                 }
  59: else
  60:                                 {
  61:                                     _wfAssociation = a;
  62:                                 }
  63:                             }
  64:                             wfList.UpdateWorkflowAssociation(_wfAssociation);
  65:                             System.Console.WriteLine("n" + wfAssoName + ": " + assoCounter.ToString() + " workflow association(s) changed successfuly!n");
  66: break;
  67:                         }
  68: default: //default number of parameters
  69:                         {
  70:                             System.Console.WriteLine("Incorrect number of arguments entered (" + args.Length.ToString() + " arguments)");
  71:                             showHelpUsage();
  72: break;
  73:                         }
  74:                 }
  75:  
  76:             }
  77: catch (Exception e)
  78:             {
  79:                 System.Console.WriteLine("An error has occurred. Details:n" + e.ToString());
  80:             }
  81: finally
  82:             {
  83: if (wfSite != null)
  84:                     wfSite.Dispose();
  85: if (wfWeb != null)
  86:                     wfWeb.Dispose();
  87:                 System.Console.WriteLine("nFinished setting AutoCleanupDays!");
  88:             }
  89:         }
  90: static void showHelpUsage() //help screen
  91:         {
  92:             System.Console.WriteLine("nnMOSS Workflow Set AutoCleanup Usage:");
  93:             System.Console.WriteLine("====================================");
  94:             System.Console.WriteLine("ShowWFs siteURL library workflow days");
  95:             System.Console.WriteLine(" - siteURL (e.g. http://serverURL/site)");
  96:             System.Console.WriteLine(" - library (e.g. "Shared Documents")");
  97:             System.Console.WriteLine(" - workflow (e.g. "Approval")");
  98:             System.Console.WriteLine(" - days for auto clean up (e.g. 120)");
  99:         }
 100:     }
 101: }