Get the item a SharePoint workflow task is associated with

by James Mann10. March 2008 13:24

This is handy. SharePoint helpfully populates the meta data with the GUID of the list and the ID of the item a WF instance is associated with. These are stored in "ows_WorkflowListId" and "ows_WorkflowItemId" fields on the task list item.

 An example of using this is from an InfoPath form in the form load code behind.

SPListItem currentListItem = SPContext.Current.ListItem;
if (currentListItem != null)
{
object associatedWfListId = currentListItem["ows_WorkflowListId"];
object associatedWfItemId = currentListItem["ows_WorkflowItemId"];
if (associatedWfItemId != null && associatedWfListId != null)
{
SPListItem item = web.Lists.GetList(new Guid(associatedWfListId.ToString()), false).GetItemById(int.Parse(associatedWfItemId.ToString()));
// THE ABOVE ITEM IS THE ASSOCIATED LIST ITEM
}
}

Tags: , ,

Infopath | MOSS | Sharepoint

Comments (9) -

Steve
Steve
3/25/2008 9:14:58 PM #

It is really good blog!

Steve
Steve
9/10/2008 9:14:56 PM #

Thanks.. Helped me today.

Martin Cohen
Martin Cohen
11/13/2008 7:42:07 PM #

This is very helpful, but you made a small mistake.  Your last statement returns a SPList object.  Once you have the list, you can use associatedWFItemId to get the item from the list.

Martin Cohen
Martin Cohen
11/13/2008 7:46:50 PM #

I withdraw my last comment.  For some reason, the full last statement is not showing up in my browser.  When I did a copy and paste, I got the full statement, which is correct.  Please accept my apology.

Steve
Steve
12/1/2008 11:01:14 AM #

Thanks for the post
can u help me to get all task of wf per item of list....

taskworkflow
taskworkflow
12/25/2008 8:35:40 AM #


Nice blog !

Good job, Thanks for the information

Steve
Steve
2/16/2009 9:48:45 AM #

Thank you for the code . It was really helpful for me.  

Steve
Steve
9/10/2009 3:26:49 PM #

Awesome.

thnx dude

Steve
Steve
10/12/2009 12:40:05 PM #

Thanks !!!!!!!!!!!!!!

Steve
Steve
10/22/2009 5:57:48 PM #

I am trying to execute same code on Button click event of infopath form..But somehow SPContext.Current.ListItem object is null.. SpContext.Current object is not null But all properties related to current list and item are not set to the value.  Can you please guide what can be the reason..i am using form library to save the forms...

Steve
Steve
7/8/2011 6:36:32 AM #

This is Fantastic. Exactly What I was searching for..

Thanks a lot James. Keep up the Good work

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading