1

I am trying to read WorkflowTaskId and WorkflowListId fields from SharePoint Workflow Task List using SSOM C# but it says column doesn't exist While I can see both columns are displayed with values in U2U CAML Builder.

Query:

 SPQuery qry = new SPQuery();
 qry.Query = Query = @"<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>3118</Value></Eq></Where>";
 DataTable dt = new DataTable();
 dt = spList.GetItems(qry).GetDataTable();

Code to read:

for(int i=0 ; i < dt.Rows.Count; i++)
{
    DataRow row = dt.Rows[i];
    string WorkflowListId = row["WorkflowListId"].ToString();
    string WorkflowTaskId = row["WorkflowTaskId"].ToString();
}

Thanks in advance.

2
  • Did you check - Is dt empty? – Raf Dec 30 '19 at 10:58
  • @Raf I can see the data is bound to dt but it is missing these two columns. – Mohammed Faisal Dec 30 '19 at 11:07
1

I suggest to use SPListItemCollection class instead of DataTable.

Please try:

using (SPSite site = new SPSite("YOUR SITE URL"))
using (SPWeb web = site.OpenWeb())
{
    SPList list = web.Lists.TryGetList("Workflow Tasks");
    var query = new SPQuery
    {
        Query = String.Format("<Where><Eq><FieldRef Name='{0}' /><Value Type='Counter'>{1}</Value></Eq></Where>",
                    "ID", 3118)
    };
    var items = list.GetItems(query);

    SPListItem item = items[0];
    string WorkflowListId = item["WorkflowListId"].ToString();
    string WorkflowTaskId = item["WorkflowTaskId"].ToString();
}

Then you can create DataTable and populate row data from variables(WorkflowListId, WorkflowTaskId).

Also you can set ViewFields property to your query.

This code is working for me:

using (SPSite site = new SPSite("YOUR SITE URL"))
using (SPWeb web = site.OpenWeb())
{
    SPList list = web.Lists.TryGetList("Workflow Tasks");
    var query = new SPQuery
    {
        Query = String.Format("<Where><Eq><FieldRef Name='{0}' /><Value Type='Counter'>{1}</Value></Eq></Where>",
                    "ID", 3118)
    };
    query.ViewFields = string.Concat(
          "<FieldRef Name='Title' />",
          "<FieldRef Name='WorkflowListId' />",
          "<FieldRef Name='WorkflowTaskId' />");
    var items = list.GetItems(query);

    DataTable dt = new DataTable();
    dt = items.GetDataTable();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DataRow row = dt.Rows[i];
        string WorkflowListId = row["WorkflowListId"].ToString();
        Console.WriteLine(WorkflowListId);
    }            
}
6
  • Hi @Raf This approach does work for me but the problem is while converting listitemcollection to Datatable as it does not show mentioned fields in datatable. – Mohammed Faisal Dec 31 '19 at 7:23
  • Hi @MohammedFaisal . As workaround you can save values of these fields in variables and then populate datatable with variables. – Raf Dec 31 '19 at 7:34
  • Hi @Raf, Currently I am doing the same implementation. But its very strange that the datatable does not show those columns. Thanks for your replies as it solved my issue. – Mohammed Faisal Dec 31 '19 at 7:35
  • plz Update your post and add some text to populate the datatable later so that I can mark it as answer. – Mohammed Faisal Dec 31 '19 at 7:39
  • Did you try to add ViewFields to your SPQery? It also works for me. – Raf Dec 31 '19 at 7:53
0

We can add ViewFields in SPQuery to solve this issue. The following code for your reference.

using (SPSite site = new SPSite("http://sp2013/sites/team"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList spList = web.Lists.TryGetList("Workflow Tasks");
        SPQuery qry = new SPQuery();
        qry.Query = @"<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>3118</Value></Eq></Where>";
        qry.ViewFields = string.Concat(
                      "<FieldRef Name='Title' />",
                      "<FieldRef Name='WorkflowListId' />",
                      "<FieldRef Name='WorkflowTaskId' />");
        DataTable dt = new DataTable();
        dt = spList.GetItems(qry).GetDataTable();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow row = dt.Rows[i];
            string WorkflowListId = row["WorkflowListId"].ToString();
            string WorkflowTaskId = row["WorkflowTaskId"].ToString();
        }
    }
}

If you want to get list item by ID, I suggest you use the code below to achieve it.

using (SPSite site = new SPSite("http://sp2013/sites/team"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList spList = web.Lists.TryGetList("Workflow Tasks");
        SPQuery qry = new SPQuery();
        SPListItem item = spList.GetItemById(3118);
        string WorkflowListId = item["WorkflowListId"].ToString();
        string WorkflowTaskId = item["WorkflowTaskId"].ToString();
    }
}
2
  • Hi @LZ_MSFT I have already tried using viewfields but still it is unable to find the columns in the list item. While when I don't convert list collection to Datatable, it returns me those fields as well. – Mohammed Faisal Dec 31 '19 at 7:22
  • Thanks for your reply. – Mohammed Faisal Dec 31 '19 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.