Monday, December 26, 2011

Delete workflow history items that has no associations in Sharepoint

Whenever the Out of the box timer job "workflow auto clean up" jobs runs, it will break the associations between the list items and workflow history items for those records that are older than 60 days (default is 60 days. this default value can be chagned). This job will not clear the history data, it just removes the association. Now it is up to us either to keep the history details or remove them. 

For those who want to remove those records can use the below sample code. We have used processbatch data method to delete items which will have better performance.

string WorkflowAssocID="";
            try
            {
                using (SPSite site = new SPSite("http://SiteURL/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["List_Name"];//Name of the list where Workflow is associated
                        SPQuery qry = new SPQuery();
                        qry.ViewAttributes = "Scope=\"Recursive\"";
                        SPListItemCollection lstCol = list.GetItems(qry);
                        SPWorkflowAssociationCollection WFCol = list.WorkflowAssociations;
                        foreach (SPWorkflowAssociation wa in list.WorkflowAssociations)
                        {
                            if (wa.Name == "Workflow Name")//name of the Workflow
                                WorkflowAssocID = wa.ParentAssociationId.ToString();
                        }
                       
                        List<string> ootbLib = new List<string>();
                        foreach (SPListItem item in lstCol)
                        {
                            if (item.Workflows.Count == 0)
                            {
                                ootbLib.Add(item.ID.ToString());
                            }
                        }
                        SPList wflist = web.Lists["Workflow History"];//History List Name
                        string wflistID = wflist.ID.ToString();
                        SPQuery query = new SPQuery();
                        query.ViewAttributes = "Scope=\"Recursive\"";
                        SPListItemCollection wflstCol = wflist.GetItems(query);
                       
                        StringBuilder batchString = new StringBuilder();
                        batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch OnError=\"Continue\">");
                        for (int i = 0; i < wflstCol.Count; i++)
                        {
                            if (ootbLib.Exists(delegate(string p) { return p.Trim() == wflstCol[i]["Primary Item ID"].ToString(); }))
                            {
                                if (wflstCol[i]["Workflow Association ID"].ToString() == "{" + WorkflowAssocID + "}")
                                {
                                   
                                    batchString.Append("<Method>");
                                    batchString.Append("<SetList Scope=\"Request\">" + wflistID + "</SetList>");
                                    batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(wflstCol[i]["ID"]) + "</SetVar>");
                                    batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
                                    batchString.Append("</Method>");
                                }
                            }
                        }
                        batchString.Append("</Batch>");
                        web.AllowUnsafeUpdates = true;
                        string result = web.ProcessBatchData(batchString.ToString());
                        web.AllowUnsafeUpdates = false;
                    }
                }
               
            }
            catch (Exception ex)
            {
                //Some logging can be done
            }

No comments:

Post a Comment