none
how to get a reference to the SharePoint 2013 workflow task?

    Question

  • I have a SPD designed SP 2013 workflow.  it creates a task based on a custom content type fine.  Now I'm creating a custom edit form template with custom buttons on it.  My custom form works fine, the button click event is caught on the server side fine.  Now I need to do something to the parent list item that the workflow starts from.  This is where I can't figure out how to do anymore.   I can get the workflow instance id in the field WF4InstanceId of the task item.  However, that's as far I could go.   It appears that none of the existing SharePoint workflow APIs can access the SP 2013 workflow instances anymore:

    SPWeb.Workflows has 0 count though one instance of my 2013 workflow is running.

    taskListItem.Tasks has 0 count too so I can't get a SPWorkflowTask instance. 

    I understand that SP 2013 workflow is completely different, but how can I go about getting a reference of the parent list item that started the workflow stance in the task edit form?  It is such a common need but appears to be impossible or unknown through the workflow APIs?

    Sunday, February 17, 2013 9:58 PM

Answers

  • Hi,

    I use the following piece of code to get reference on related item:

    string relatedItems = SPContext.Current.Item["Related Items"] + "";
    Guid listId;
    SPItem relatedItem = GetRelatedItem(SPContext.Current.Web, relatedItems, out listId);

    public SPItem GetRelatedItem(SPWeb web, string relatedItems, out Guid listID)
    {
        //TODO don't forget to catch possible exceptions
        relatedItems = relatedItems.Trim(new char[] { '[', '{', '}', ']' });
        string[] relatedItemsVals = relatedItems.Split(',');
        Dictionary<string, string> items = new Dictionary<string, string>();
        foreach (var itemsVal in relatedItemsVals)
        {
             string[] keyval = itemsVal.Split(':');
             items[keyval[0].Trim('"')] = keyval[1].Trim('"');
        }
    
        string itemIDStr = items["ItemId"];
        string listIDStr = items["ListId"];
        listID = new Guid(listIDStr);
        SPList itemList = web.Lists[listID];
        SPItem item = itemList.GetItemById(int.Parse(itemIDStr));
        return item;
    }

    It seems that there should be a better way to do this, but for the moment it is the only method which I have found and it works well in my cases. Unfortunately, there are almost no articles and documentation how to work with new WF2013 platform.

    Hope it helps. If so, please press upvote and mark as answer :-).

    PS: If somebody knows better way, please, let us know.

    Regards, Michael (http://sp2013-blog.com).





    • Marked as answer by SquareWheel Monday, February 18, 2013 6:38 PM
    • Edited by MikhailSP Saturday, March 09, 2013 9:54 PM
    Monday, February 18, 2013 6:06 AM
  • Michael:

    Thanks a lot. Yes I found out that the Related Items field contains the info in the wee hours this morning.  And I'm with you, this is such a bad way to convey this information as the field itself is editable by default (I made it hidden so it can't be edited in my form). 

    The hidden field WF4InstanceId contains the right information too.  I think it is the correct source to get information on the workflow instance information.  Unfortunately, there is no API/infrastructure to get to that.  In my opinion, the whole 2013 workflow thing was far from being ready when RTM was released.  It is still almost impossible to develop any serious workflows in Visual Studio.  

    • Marked as answer by SquareWheel Monday, February 18, 2013 6:38 PM
    Monday, February 18, 2013 6:38 PM

All replies

  • Hi,

    I use the following piece of code to get reference on related item:

    string relatedItems = SPContext.Current.Item["Related Items"] + "";
    Guid listId;
    SPItem relatedItem = GetRelatedItem(SPContext.Current.Web, relatedItems, out listId);

    public SPItem GetRelatedItem(SPWeb web, string relatedItems, out Guid listID)
    {
        //TODO don't forget to catch possible exceptions
        relatedItems = relatedItems.Trim(new char[] { '[', '{', '}', ']' });
        string[] relatedItemsVals = relatedItems.Split(',');
        Dictionary<string, string> items = new Dictionary<string, string>();
        foreach (var itemsVal in relatedItemsVals)
        {
             string[] keyval = itemsVal.Split(':');
             items[keyval[0].Trim('"')] = keyval[1].Trim('"');
        }
    
        string itemIDStr = items["ItemId"];
        string listIDStr = items["ListId"];
        listID = new Guid(listIDStr);
        SPList itemList = web.Lists[listID];
        SPItem item = itemList.GetItemById(int.Parse(itemIDStr));
        return item;
    }

    It seems that there should be a better way to do this, but for the moment it is the only method which I have found and it works well in my cases. Unfortunately, there are almost no articles and documentation how to work with new WF2013 platform.

    Hope it helps. If so, please press upvote and mark as answer :-).

    PS: If somebody knows better way, please, let us know.

    Regards, Michael (http://sp2013-blog.com).





    • Marked as answer by SquareWheel Monday, February 18, 2013 6:38 PM
    • Edited by MikhailSP Saturday, March 09, 2013 9:54 PM
    Monday, February 18, 2013 6:06 AM
  • Michael:

    Thanks a lot. Yes I found out that the Related Items field contains the info in the wee hours this morning.  And I'm with you, this is such a bad way to convey this information as the field itself is editable by default (I made it hidden so it can't be edited in my form). 

    The hidden field WF4InstanceId contains the right information too.  I think it is the correct source to get information on the workflow instance information.  Unfortunately, there is no API/infrastructure to get to that.  In my opinion, the whole 2013 workflow thing was far from being ready when RTM was released.  It is still almost impossible to develop any serious workflows in Visual Studio.  

    • Marked as answer by SquareWheel Monday, February 18, 2013 6:38 PM
    Monday, February 18, 2013 6:38 PM
  • Hi,

    I absolutely agree with you. I really get upset about current state of 2013 WF platform. Several really serious bugs (like unable to deploy new version of WF), almost no documentation about its API, lack of support on forums (as a consequence of the lack of documentation, as nobody except MS SP team knows new features, but they don't help on forums).

    Really hope it will change soon.

    Regards, Michael (http://sp2013-blog.com).






    • Edited by MikhailSP Saturday, March 09, 2013 9:53 PM
    Monday, February 18, 2013 6:51 PM