Going to take a quick break from my BCS series to talk about a request I just completed for a client. I wanted to provide an alternative method for SharePoint workflow history retention.  If a person needs to see what happened during the course of a workflow for an item they simply need to go to the item and view the link to the workflow from there to see the entire history laid out for them. However, as we all know; SharePoint will purge any completed or terminated workflows from a site. So if a user is to try and view the data after 60 days, they will not be able to see the summary any more. For some this is a big deal as they need to track that data for years.

What some may not know is that SharePoint hasn’t actually removed the data from the system. It’s all still there in the workflow history and task lists. However, what SharePoint does is actually removes the references between the list item and the workflow history. This is done so the references that SharePoint has to scan don’t grow too large and cause performance issue.

So what’s a solution? One solution is to un-hide the workflow history and allow users to view the list directly. Not what I would suggest. Does this look very user friendly to you?

p_WFHistory_PicOfWFHistory

A user is not going to know how to view the corresponding list and item information from a bunch of GUIDs.

So what can we do?  There are numerous posts out there on turning off the job that performs the retention or moving the retention date from the default of 60 days out for years.  These are valid options, though if you can go along without doing this you are decreasing your chances of having performance issues down the road.

I suggest the following alternative.  Create a backup history list.  It retains all the information from the workflow history, but is user friendly so they can link quickly to the item and the list the workflow was initiated from.  The solution I built is a PowerShell script that allows you to input a data parameter to grab any history items from that day forward, and has a config file that contains the sites and the different history lists (in case you have custom) that you want to retain.

While doing my research I found that custom workflows do not store all the same data as an OOTB workflow does.  If you take a look at that screenshot above again, notice the items where the Workflow Association ID is NOT a GUID and it is missing data from other columns?  Those are custom WF entries.  What that means, is we do not have an easy way of getting the list or the list item to reference back for the user.  But fret not!  I have a solution!

If it is a custom WF, grab that Workflow History Parent Instance ID and move through the lists of your site.  At each site, check to see if the list contains a column whose name matches the parent instance ID.  If it does, get the internal (static) name of that column and check to see if Workflow History Parent Instance GUID is found for one of the items.  This is done quite easily with a SPQuery.

Here’s the code you need:

 

BuildFindItemWFParent simply returns a SPQuery that looks like this:

Once you have this information it’s a simple step to build out your data fields and list items.  One thing I would like to mention.  When building your link back to the parent list and parent item, I suggest you use a hyperlink column so it can contain the title and the link.  To update a Hyperlink field you have to include both the URL and the title in the string like this:

When you are done with building your list you will end up with something like this:

p_WFHistory_PicOfBKPWFHistory

Still pretty ugly, so create a view that cleans it up and is more user friendly:

p_WFHistory_PicOfBKPWFHistoryClean

That should have everything you need to implement this alternative method.  Adding OOTB WF history entries is easy since you have the GUIDs for the list and ID for the item.  I haven’t finished cleaning up the source code for this which is why it isn’t posted.  Let me know if you need it and I will get it to you.

The config file is pretty straight forward:

As you can see it contains the list information, where the source history list is and where you want to put the history backup. It also contains the cross reference of WF History columns and the ones I created. It also has a cross reference of the events that occurred so the user knows what happened specifically in the workflow history entry.

Once you have the script completed, you just need to add it to the Task Scheduler on the server so it runs automatically.  If you don’t run it regularly (monthly) then the Workflow History Parent Instance GUID will be removed from the list by the retention clean-up and you won’t be able to gather all the information.

Thanks for reading!