First published on TECHNET on Dec 17, 2012
This blog post is a contribution from Balaji Sadasivan, an engineer with the SharePoint Developer Support team.
Issue
In order to audit workflows, we require to see the name of the workflow in the history list.
Cause
Currently the workflow history has no provision to show the workflow name.
Workaround
Create a new workflow history list from the schema of the original workflow history list. To the workflow history list add an ItemAdded even handler. In that write the flowing code:
SPListItem item = properties.ListItem;
SPSite site = new SPSite("http://<Servername>");
SPWeb web = site.RootWeb;
SPList destination = web.Lists["CustWFHistory"];//Custom workflow list
SPList wrkFlwList = web.Lists["MyList"];//The list where the workflow is located
string assocID = item["Workflow Association ID"].ToString();//Copying values from source workflow history
string templateID = item["Workflow Template ID"].ToString();
string listID = item["List ID"].ToString();
string primaryID = item["Primary Item ID"].ToString();
DateTime dateOccured = (DateTime)item["Date Occurred"];
Guid instance=new Guid(assocID);
SPWorkflowAssociationCollection WFCol = wrkFlwList.WorkflowAssociations;
SPListItem newItem = destination.Items.Add();
newItem["Workflow Association ID"] = assocID;//Adding them to destination
newItem["Workflow Template ID"] = templateID;
newItem["List ID"] = listID;
newItem["Primary Item ID"] = Convert.ToInt32(primaryID);
newItem["Date Occurred"] = dateOccured;
foreach (SPWorkflowAssociation wa in wrkFlwList.WorkflowAssociations)
{
string silID = wa.Id.ToString();
assocID = assocID.Replace("{", "");
assocID = assocID.Replace("}", "");
assocID = assocID.Trim();
if(silID==assocID)
newItem["Workflow name"] = wa.Name;//Getting the workflow name
}
newItem.Update();
site.Dispose();
The new list will show up with the workflow name, workflow association ID, List ID, date occurred. You can add fields like user ID etc. to the custom list query it from the workflow history list and add it.