How to get item approval workflow status using sharepoint object model?

There is a list that the approval workflow is associated with, the workflow starts when the item is added. The list has anonymous access so that everyone can enter an item, but this item must be approved by the approver. the item will have a status (for example, approved, rejected, etc.).

Now I am creating a visual website. I want to display list items in a grid. But I need to display only those items that are approved. I am using the sharepoint object model. How to filter items based on approval status.

thanks

+6
visual-studio-2010 sharepoint-2010
source share
4 answers

The name of the Workflow Status field is usually the first 8 alphanumeric characters of the name of your workflow. The Approved value is 16. Therefore, the selection of only approved items should look something like this:

string caml = @"<Where><Eq><FieldRef Name='MyApprov' /><Value Type='WorkflowStatus'>16</Value></Eq></Where>"; SPQuery query = new SPQuery(); query.Query = caml; SPListItemCollection items = list.GetItems(query); 
+2
source share
 private static void ListWorkflowStatus() { string siteUrl = ConfigurationManager.AppSettings["SiteUrl"]; // Get the site in impersonated context using (SPSite site = new SPSite(siteUrl)) using (SPWeb web = site.OpenWeb()) { SPList lib = web.Lists[ConfigurationManager.AppSettings["LibraryName"]]; foreach (SPListItem item in lib.Items) { string titleColumn = ConfigurationManager.AppSettings["TitleColumn"]; string workflowColumn = ConfigurationManager.AppSettings["WorkflowColumn"]; string workflowStatus = item[workflowColumn] as string; Console.WriteLine(string.Format("{0}\t{1} ({2})", item[titleColumn], GetStatusName(workflowStatus), workflowStatus)); } } } private static string GetStatusName(string value) { if (string.IsNullOrEmpty(value)) { return "N/A"; } switch (value) { case "0": return "Not Started"; case "1": return "Failed On Start"; case "2": return "In Progress"; case "3": return "Error Occurred"; case "4": return "Canceled"; case "5": return "Completed"; case "6": return "Failed On Start (retrying)"; case "7": return "Error Occurred (retrying)"; case "15": return "Canceled"; case "16": return "Approved"; case "17": return "Rejected"; default: return "N/A"; } } 

The only problem is how to get the column name of the workflow; for this, I suggest you use the SharePoint Manager 2010 tool, go to the library with which the workflow is associated, and get the column name from there (it will be 8 characters, for me it was "AOFWF0")

+2
source share

You can get status using this code example

Code example:

 //Obtain a list item SPListItem currItem = currList.Items[0]; //Get the workflow status value int wFlowStatusnum= (int)currItem ["WorkFlowName"]; //this will give you the one you see in the List view string wFlowStatus= System.Enum.GetName(typeof(SPWorkflowStatus), wFlowStatusnum); 
+1
source share

I came to the page looking for an answer, but found that all answers use a static field name or some other approach that is very limited.

So, finally do my own restart, and this is how I form the name of the workflow field

 string WFColumnName = string.Empty; foreach (SPWorkflowAssociation assoc_wf in ReqList.WorkflowAssociations) { if (assoc_wf.BaseTemplate.Id.ToString() == "63e32cd3-2002-4b2f-81b0-4a2b4c3ccafa") { string str_workflowName = assoc_wf.Name; Regex rgx = new Regex("[^a-zA-Z0-9]"); str_workflowName = rgx.Replace(str_workflowName, ""); if (str_workflowName.Trim().Length >= 8) WFColumnName = str_workflowName.Substring(0, 8); else WFColumnName = str_workflowName.Substring(0, str_workflowName.Length); break; } } 

The column name is formed based on this (the name is misleading, but it's just the column name): How to get the status of a SharePoint workflow for each SP element using a PowerShell script?

and then i use it in my caml request

 if(WFColumnName != string.Empty) viewFields += "<FieldRef Name='" + WFColumnName + "' />"; 

and in my case, I just need to check if it is complete or not, so I get the string value and compare it with "5".

0
source share

All Articles