Virtual Business Card


A Jack of all trades is a master of integration, as such an individual who knows enough from many learned trades and skills to be able to bring their disciplines together in a practical manner, and is not a specialist but can be an expert in many fields. Such a person is known as a polymath or a renaissance man; a typical example is someone like Leonardo da Vinci.

Tuesday, October 11, 2011

Accessing Workflow Status in a List

I have a CAML Query below that queries a list where the workflow for an item is not cancelled.  There's an extra column in the list called Recruitment Workflow which is of type WorkflowStatus.  This column is somehow automatically added by SharePoint.  I tried to query using CAML using the query below:
Note That I'm using the Internal Name for Recruitment Workflow Column in this query.
<Query>
       <Where>
                  <Neq>
                          <FieldRef Name="Recruitm" />
                          <Value Type="WorkflowStatus">Canceled</Value>
                 </Neq>
         </Where>
</Query>

Weird thing is I'm not getting any results.
I also tried to query everything to test my insanity and query and test if the column exsist:
foreach (SPListItem item in recruitmentrequests)
{
                item[“Recruitment Workflow”] =
               ….
}
It fails on  the underlined code and I got the error


item["Recruitment Workflow"]' threw an exception of type 'System.ArgumentException'
    base {System.SystemException}: {"Value does not fall within the expected range."}
    Message: "Value does not fall within the expected range."
    ParamName: null

I also replaced the code with  item[“Recruitm”] =

and I still got the error above.  I also tried item["Recruitment_x0020_ Workflow"];

and it generates an error saying that column does not exist.
I also checked via code if that column exists using item.Fields["Recruitment Workflow"] and it can retrieve it.

So this is really puzzling me.  The column is there but I cannot query it to get its value using CAML or .NET code.


It looks like accessing a workflow status is a special case and cannot be accessed via SPListItem["WorkflowStatusColumnName"].  You can access it using the snippet below:

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
// This is Console Application //
SPSite site = new SPSite("http://sharepoint-site/");
SPWeb web = site.OpenWeb();
foreach (SPList list in web.Lists)
{
    foreach (SPListItem item in list.Items)
    {
        foreach (SPWorkflow workflow in item.Workflows)
        {
            try
            {
                Console.WriteLine("item url : " + item.Url);
                Console.WriteLine("workflow name : " + list.WorkflowAssociations[workflow.AssociationId].Name);
                Console.WriteLine("workflow status : " + item[list.WorkflowAssociations[workflow.AssociationId].Name]);
                Console.WriteLine();
            }
            catch (ArgumentException)
            {
                //ArgumentException is throwed if the workflow is not exist in the list column.
                Console.WriteLine("Workflow name : {0} is already not exist in List title : {1}.",
                    list.WorkflowAssociations[workflow.AssociationId].Name, list.Title);
            }
        }
    }
}



No comments:

Post a Comment