Monday, December 26, 2011

Workflow Auto clean job property in Sharepoint

You can check Workflow Auto clean job property by using the following stsadm command 
stsadm –o setproperty –pn job-workflow-autoclean –pv “daily at 6:34:00” –url http://siteurl

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
            }

Document version size in Sharepoint

The below code will calculate the document version size for each item for a single document library.

using (SPSite site = new SPSite("http://Site_URL/")) //Specify your SiteURL
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList objList = web.Lists["Doclib"];//Document library Name
                    SPQuery objQuery = new SPQuery();
                   
                    objQuery.ViewAttributes = "Scope=\"Recursive\"";

                    SPListItemCollection objItemColl = objList.GetItems(objQuery);
                    foreach (SPListItem objItem in objItemColl)
                    {
                        long versionSize = 0;

                        SPListItemVersionCollection objVerisionColl = objItem.Versions;

                        foreach (SPListItemVersion objVersion in objVerisionColl)
                        {
                            int versionID = objVersion.VersionId;
                            string strVersionLabel = objVersion.VersionLabel;

                            SPListItem objLstItm = objVersion.ListItem;
                            versionSize = versionSize + objLstItm.File.Length;
                           
                        }
                        Console.WriteLine("Item:" + objItem.Name + " Version Count:" + objVerisionColl.Count + " Size:" + versionSize.ToString());
                       

                    }
                }
            }