Tuesday, October 21, 2014

Check Workflow association of list items

Hi,

I was researching on this matter and found a way implement. I got event handler for my submit button and I have added my code inside it.

In my test, I'll be getting site collection url from a textbox and evaluate all the webs inside that site collection. We will go inside web object and get all the lists and evaluate whether any item associated with a workflow that running status as 2, which is "In progress". Code is below

            string SiteUrl = string.Empty;
            ArrayList inflightWorkflowDocuments = new ArrayList();
 
            if (string.IsNullOrEmpty(txtSiteUrl.Text.ToString()))
            {
                throw new Exception("Site URL is empty!");
            }
            else
            {
                SiteUrl = txtSiteUrl.Text.ToString();
            }
 
            //Check site collection exist or not
 
            bool isSteExist = SPSite.Exists(new Uri(SiteUrl));
 
            if (isSteExist && !string.IsNullOrEmpty(SiteUrl))
            {
                using (SPSite site = new SPSite(SiteUrl))
                {
                    //Loop through all webs
                    SPWebCollection allWebs = site.AllWebs;
                    Dictionary<stringstring> liUrls = new Dictionary<stringstring>();
 
                    foreach (SPWeb theWeb in allWebs)
                    {
                        Guid webGuid = theWeb.ID;
                        using (SPWeb web = allWebs[webGuid])
                        {
                            SPListCollection cusLists = null;
                            if (null != web.Lists)
                            {
                                //Get list of lists (custom/OOB lists)
                                cusLists = web.Lists;
                            }
 
                            if (null != cusLists)
                            {
                                foreach (SPList sltList in cusLists)
                                {
                                    if (sltList.Title == "TestList")
                                    {
                                        //Get each list and iterate through all items on that list
                                        foreach (SPListItem litem in sltList.Items)
                                        {
 
                                            //Check all workflows and their associations
                                            int wkflStatus = GetWorkflowStatus(litem);
 
                                            /*   Workflow statuses
                                     
                                                    Not Started                     0
                                                    Failed on Start                 1
                                                    In Progress                     2
                                                    Error Occurred                  3
                                                    Canceled                        4
                                                    Completed                     5
                                                    Failed on Start (retrying)     6
                                                    Error Occurred (retrying)     7
                                                    Canceled                     15
                                                    Approved                     16
                                                    Rejected                     17
                                                */
 
                                            //Check that list item having Workflow 'In Progress'
                                            if (wkflStatus == 2)
                                            {
                                                //Rest of your work                                               
                                            }
                                        }
 
                                    }
                                    
                                }
                            }
                        }
                    }
                }
            }
 
            
            //Creating a grid view with items having workflow associated.
 
            if (null != inflightWorkflowDocuments)
            {
                grdWorkflowItems.DataSource = (string[])inflightWorkflowDocuments.ToArray(typeof(string));
                grdWorkflowItems.DataBind();            
            }
}


public int GetWorkflowStatus(SPListItem item)
        {
            if (item == null)
                return -1;
 
            SPWorkflowManager manager = item.Web.Site.WorkflowManager;
            foreach (SPWorkflow instance in manager.GetItemWorkflows(item))
            {
                if ((instance != null) && (instance.ParentList != null) && (instance.ParentList.Fields != null))
                {
                    foreach (SPField field in instance.ParentList.Fields)
                    {
                        if ((field != null) && (field is SPFieldWorkflowStatus))
                        {
                            SPFieldWorkflowStatus statusField = (SPFieldWorkflowStatus)field;
                            if (item[statusField.StaticName] != null)
                            {
                                int statusValue = -1;
                                try
                                {
                                    statusValue = int.Parse(item[statusField.StaticName].ToString());
                                }
                                catch { }
                                return statusValue;
                            }
                        }
                    }
                }
            }
            return -1;
        }

No comments:

Post a Comment