Friday, June 29, 2012

Workflow Association Auto cleanup

Recently I have a requirement to keep the workflow history details. First I had doubt to use the "AutoCleanUp" properties of the "WorkflowAssociations" class.Because the document library was attached to an out of box workflow - SharePoint Approval Workflow."

To confirm this doubt I have created the new document library and attached to out of box workflow.My workflow's unique name was "DemoWF". Couple of year before I have done this to my custom workflow which was created through Visual Studio.

After this workaround,I made sure it can be work for OOTB workflow too.

Created a visual webpart to get all the workflow association to this document library.I am able to get my workflow's by default auto clean up days 60.

Now I set the "AutoCleanup" value as per my desire.

On my page,I have created UI to load all List type templates to see the workflow association associated in them.
if (!Page.IsPostBack)
            {
                SPSite site = SPContext.Current.Site;
                SPWeb web = SPContext.Current.Web;
                SPListCollection collection = web.Lists;

                foreach (SPList list in collection)
                {
                    if (list.BaseType == SPBaseType.DocumentLibrary)
                    {
                        SPDocumentLibrary docLib = (SPDocumentLibrary)list;

                        if (!docLib.IsCatalog && docLib.BaseTemplate != SPListTemplateType.XMLForm)
                        {
                            ddlList.Items.Add(docLib.Title.ToString());
                        }
                    }


                }
            }
On selected Index change value of my loaded lists/document library,wire up with events to know the specific workflows for the list or document library,
 protected void ddlList_SelectedIndexChanged1(object sender, EventArgs e)
        {
            ListBox1.Items.Clear();
            ListBox1.Items.Insert(0, "--Select--");
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            SPList docLib = (SPDocumentLibrary)web.Lists[ddlList.SelectedItem.Text];

            SPWorkflowAssociationCollection cols = docLib.WorkflowAssociations;
            if (cols.Count > 0)
            {
                foreach (SPWorkflowAssociation wfa in docLib.WorkflowAssociations)
                {
                    ListBox1.Items.Add(wfa.Name);
                   //Loads the worflow names in the given list box.  
                                    
                   
                }

            }
            
        }
when you select the specific workflow name from the list box,It will shows you the default or extended auto clean up day in the literal control.
 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            SPList docLib = (SPDocumentLibrary)web.Lists[ddlList.SelectedItem.Text];
            string workflowName = String.Empty;
            SPWorkflowAssociationCollection cols = docLib.WorkflowAssociations;
            foreach (SPWorkflowAssociation wfa in docLib.WorkflowAssociations)
            {
                if (wfa.Name.Equals(ListBox1.SelectedItem.Text))
                {
                    Literal1.Text = "AutoCleanup for this workflow : " + wfa.AutoCleanupDays.ToString();
                }
            }
        }
OnClick event of the button will simply extend the AutoCleanUp duration of the specific workflow's association. So the Workflow History and Task history will be longer as you extended here.
protected void btnExtend_Click(object sender, EventArgs e)
        {
            SPSite site = SPContext.Current.Site;
            SPWeb web = SPContext.Current.Web;
            
            SPList docLib = (SPDocumentLibrary)web.Lists[ddlList.SelectedItem.Text];
            string workflowName = String.Empty;
      
                foreach (SPWorkflowAssociation wfa in docLib.WorkflowAssociations)
                {
                    workflowName = wfa.Name;
                    
                    if (workflowName ==ListBox1.SelectedItem.Text)
                    {
                     
                    wfa.AutoCleanupDays=Convert.ToInt32(TextBox1.Text.ToString());
                    docLib.WorkflowAssociations.Update(wfa);
                    }
                  
           
                }
             
                
        }