%3CLINGO-SUB%20id%3D%22lingo-sub-509712%22%20slang%3D%22en-US%22%3EWorkflow%20name%20in%20workflow%20history%20list%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509712%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Dec%2017%2C%202012%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThis%20blog%20post%20is%20a%20contribution%20from%20Balaji%20Sadasivan%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%0A%3CP%3EIssue%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20audit%20workflows%2C%20we%20require%20to%20see%20the%20name%20of%20the%20workflow%20in%20the%20history%20list.%3C%2FP%3E%0A%3CP%3ECause%3C%2FP%3E%0A%3CP%3ECurrently%20the%20workflow%20history%20has%20no%20provision%20to%20show%20the%20workflow%20name.%3C%2FP%3E%0A%3CP%3EWorkaround%3C%2FP%3E%0A%3CP%3ECreate%20a%20new%20workflow%20history%20list%20from%20the%20schema%20of%20the%20original%20workflow%20history%20list.%20To%20the%20workflow%20history%20list%20add%20an%20ItemAdded%20even%20handler.%20In%20that%20write%20the%20flowing%20code%3A%3C%2FP%3E%0A%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%0A%3CDIV%20id%3D%22codeSnippet%22%3ESPListItem%20item%20%3D%20properties.ListItem%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPSite%20site%20%3D%20new%20SPSite(%22http%3A%2F%2F%3CSERVERNAME%3E%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPWeb%20web%20%3D%20site.RootWeb%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPList%20destination%20%3D%20web.Lists%5B%22CustWFHistory%22%5D%3B%2F%2FCustom%20workflow%20list%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPList%20wrkFlwList%20%3D%20web.Lists%5B%22MyList%22%5D%3B%2F%2FThe%20list%20where%20the%20workflow%20is%20located%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20assocID%20%3D%20item%5B%22Workflow%20Association%20ID%22%5D.ToString()%3B%2F%2FCopying%20values%20from%20source%20workflow%20history%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20templateID%20%3D%20item%5B%22Workflow%20Template%20ID%22%5D.ToString()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20listID%20%3D%20item%5B%22List%20ID%22%5D.ToString()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20primaryID%20%3D%20item%5B%22Primary%20Item%20ID%22%5D.ToString()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EDateTime%20dateOccured%20%3D%20(DateTime)item%5B%22Date%20Occurred%22%5D%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EGuid%20instance%3Dnew%20Guid(assocID)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPWorkflowAssociationCollection%20WFCol%20%3D%20wrkFlwList.WorkflowAssociations%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPListItem%20newItem%20%3D%20destination.Items.Add()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EnewItem%5B%22Workflow%20Association%20ID%22%5D%20%3D%20assocID%3B%2F%2FAdding%20them%20to%20destination%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EnewItem%5B%22Workflow%20Template%20ID%22%5D%20%3D%20templateID%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EnewItem%5B%22List%20ID%22%5D%20%3D%20listID%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EnewItem%5B%22Primary%20Item%20ID%22%5D%20%3D%20Convert.ToInt32(primaryID)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EnewItem%5B%22Date%20Occurred%22%5D%20%3D%20dateOccured%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eforeach%20(SPWorkflowAssociation%20wa%20in%20wrkFlwList.WorkflowAssociations)%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Estring%20silID%20%3D%20wa.Id.ToString()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EassocID%20%3D%20assocID.Replace(%22%7B%22%2C%20%22%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EassocID%20%3D%20assocID.Replace(%22%7D%22%2C%20%22%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EassocID%20%3D%20assocID.Trim()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eif(silID%3D%3DassocID)%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EnewItem%5B%22Workflow%20name%22%5D%20%3D%20wa.Name%3B%2F%2FGetting%20the%20workflow%20name%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EnewItem.Update()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Esite.Dispose()%3B%3C%2FSERVERNAME%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThe%20new%20list%20will%20show%20up%20with%20the%20workflow%20name%2C%20workflow%20association%20ID%2C%20List%20ID%2C%20date%20occurred.%20You%20can%20add%20fields%20like%20user%20ID%20etc.%20to%20the%20custom%20list%20query%20it%20from%20the%20workflow%20history%20list%20and%20add%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509712%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20TECHNET%20on%20Dec%2017%2C%202012%20This%20blog%20post%20is%20a%20contribution%20from%20Balaji%20Sadasivan%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-509712%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%20Support%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

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.