locked
Getting list item associated with workflow task item RRS feed

  • Question

  • Hello All,

    I have task list item object (SPListItem) and I want to get ID of the list item associated with this task

    Thank you and best regards .....

    Thursday, July 4, 2013 3:12 PM

Answers

  • Thanks Yaoyao,

    It works after I removed spaces from the column names:

    taskItem["WorkflowListId"]
    taskItem["WorkflowItemId"]

    Thanks again ...

    Thursday, July 11, 2013 8:44 AM

All replies

  • Hello,

    Here is code to get associate workflow and tasks for an item:

    http://sharepointologic.blogspot.in/2010/11/programmatically-approve-sharepoint.html 

    One small change in code to get item id is (see "var item = list.Items.Cast<SPListItem>().First(c => c.ID == 1);" line):

    var item = list.Items;
    (SPLIstItem itm in item)
    {
    int id = itm.ID;
    }
    Hope it could help



    Hemendra: "Yesterday is just a memory,Tomorrow we may never see"

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    Please feel free to unmark answer if does not resolves your problem.

    Friday, July 5, 2013 7:10 AM
  • Hi Loaytrablsi,

    Task list exposes few properties which are hidden which tells the exact workflow information.
    Workflow List ID (This represents the document library ID from where the workflow was initiated)
    Workflow Item ID (This represents the document for which the work flow is initiated)
    ows_WorkflowInstanceID (This is the workflow instance id of the workflow that has been setup for document library. This will help us to get the workflow type)

    So based on the workflow item id we can get it.

    For more detailed information, please refer to

    http://mossytips.blogspot.com/2008/08/how-to-get-workflow-instance-of-task.html

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Friday, July 5, 2013 7:11 AM
  • Thanks for your replies but my case is different, I am querying The task list like this:

    SPSite site = GetSite();
                SPWeb web = site.OpenWeb();
                SPList tasksList = web.Lists["WorkflowTasks"];
                SPListItemCollection items = tasksList.GetItems();
                if (items.Count > 0)
                {
                    // Here I have this object "items[0]" and I want to get the original list item object.
                }
    I have task list item object and I want to get the original list item object

    Wednesday, July 10, 2013 2:00 PM
  • Hi idan,

    Please check the following code, and notice if the original list item has been deleted it will cause exception.

    using (SPSite site = new SPSite("http://site collection url"))
                {
                    using (SPWeb web = site.OpenWeb("sub site name"))
                    {
                        SPList list=web.Lists["Worklfow Tasks name"];
                        SPListItemCollection itemCol=list.GetItems();
                        foreach (SPListItem taskItem in itemCol)
                        {
                            //you don't need to modify the workflow list/item id, leave it is
                            if (taskItem["Workflow List ID"] != null)
                            {
                                Guid sourceListID = new Guid(taskItem["Workflow List ID"].ToString());
                                SPList sourceList = web.Lists.GetList(sourceListID, true);
                                Console.WriteLine(sourceList.Title);
                                int sourceListItemID = Convert.ToInt32(taskItem["Workflow Item ID"]);                   
                                SPListItem sourceListItem = sourceList.GetItemById(sourceListItemID);
                                Console.WriteLine(sourceListItem.Title);
                            }
    
                        }
                     Console.ReadLine();
                    }
                }

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Thursday, July 11, 2013 2:37 AM
  • Hi Yaoyao Chen,

    The column "Workflow List ID" does not exist in the tasks list. I made sure that the original list item is not deleted. It throw an exception at this line

    if (taskItem["Workflow List ID"] != null)
    The exception says : "Column 'Workflow List ID' does not exist. It may have been deleted by another user."



    Thursday, July 11, 2013 8:09 AM
  • Hi,

    I have the above code in a console application, it works well. Did you use the right Tasks list?

    Best Regards.


    Kelly Chen
    TechNet Community Support

    Thursday, July 11, 2013 8:19 AM
  • I am using the default task list in my subsite, its name "WorkflowTasks" ....

    I am using publishing subsite on sharepoint 2010 ....

    Thank you ...

    Thursday, July 11, 2013 8:27 AM
  • Thanks Yaoyao,

    It works after I removed spaces from the column names:

    taskItem["WorkflowListId"]
    taskItem["WorkflowItemId"]

    Thanks again ...

    Thursday, July 11, 2013 8:44 AM
  • Hi,

    That's so strange, it works well for me. And without space not worked for me. What's the version of your SharePoint server?

    Best Regards.


    Kelly Chen
    TechNet Community Support


    Friday, July 12, 2013 12:55 AM