To check if a List Item in a SharePoint list has been approved from all the workflows attached to it

Well, in this blog post we are going to see how to check if an SPListItem has any workflows are associated to it and are not approved. I’am not sure if there are any blogs available to do this since I did this on my own ūüėÄ .

I did this using the Workflow object model. Let us start looking at the coding.

1. First, we need to get the workflows that are associated with the list using the WorkflowAssociations class and store it in a SPWorkflowAssociationCollection object as below.

SPWorkflowAssociationCollection wfAssColl = olist.WorkflowAssociations;

2. Now, let’s loop through each workflow in the wfAssColl (after checking if the count is not 0) to get the Field that represents the workflow and also the static name of the field.

             if (wfAssColl.Count!= 0)
                   {
                       foreach (SPWorkflowAssociation wflow in wfAssColl)
                       {
                           SPFieldWorkflowStatus fld = (SPFieldWorkflowStatus)ilist.Fields.GetField(wflow.Name);

3. Get the SPListItem’s value in the corresponding workflow field that was retrieved above. Check if the value is equal to “16”.

                         if (Convert.ToString(oitem[fld.StaticName]) != "16")
                          return false;
                        }
                     }

4. If the value is not equal to 16 then the workflow for the item is not yet approved and returns false.

Here is the complete code:

SPWorkflowAssociationCollection wfAssColl = olist.WorkflowAssociations;
 if (wfAssColl.Count!= 0)
                   {
                       foreach (SPWorkflowAssociation wflow in wfAssColl)
                       {
                           SPFieldWorkflowStatus fld = (SPFieldWorkflowStatus)ilist.Fields.GetField(wflow.Name);
                          if (Convert.ToString(oitem[fld.StaticName]) != "16")
                            return false;
                        }
                     }

Hope this is helpful for anyone if at all anyone is in need of such a solution. Thank you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s