There is a list on which an approval workflow is associated,workflow starts when an item is added.The list is having anonymous access so anyone can enter an item,but the item needs to be approved by an approver.Hence,each item will have a status(e.g. approved,rejected etc.).
Now,I am creating a visual webpart.I want to display the list items in a grid.B开发者_JS百科ut I need to display only those items which are approved.I am using sharepoint object model,How can I filter the items based on the approval status.
Thanks
The name of the Workflow Status field is usually first 8 alphanumeric characters of the name of your Workflow. The value of Approved is 16. So selecting 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);
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 workflow column name; for this I suggest you use the SharePoint Manager 2010 tool, browse to the library to which the workflow is associated and get the column name from there (it will be 8 characters max, for me it was "AOFWF0")
You can get the status using this sample code
Sample Code:
//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);
I came to the page searching for the answer but found that all the answers are using static field name or some other approch which is very limiting.
So end up doing my own reserach and this is how I form the workflow field name
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(Name is misleading but it is just about the name of column): How to get the SharePoint workflow status for each SP item with a PowerShell script?
and then I use it inside my CAML query
if(WFColumnName != string.Empty)
viewFields += "<FieldRef Name='" + WFColumnName + "' />";
and in my case I just need to check if it completed or not so I get string value and compare with "5".
精彩评论