another technical blog...technically

Monday, December 8, 2014

SharePoint 2013 workflow task related item field

Another day spent with SP2013 workflows.
Do you remember when you had a task on SP2010 and you could get the workflow related item easily?
Good, it was something like that
taskItem["WorkflowItemId"]
Today, the game has changed.
Let's see this task view screenshot, everything lets you think you only have to do something like this
taskItem[SPBuiltInFieldId.RelatedItems]
to get a SPListItem, unfortunally you have to do more than this.
This field contains a JSON string containing web, list and item id, so you have to adapt this field value in order to do some work from code behind...

RelatedItem is one of the new site column SP2013 available for developers and can be used for other purposes, not only in workflows; so i decided to write this little helper in VLibs which lets you to read/write this field easily from server side, since CSOM supports this field.
[Serializable]
public class RelatedItemFieldValue
{
 #region Properties
 public int ItemId { get; set; }
 public Guid WebId { get; set; }
 public Guid ListId { get; set; }
 #endregion
} 
Then using this helper you can read and update this field value.
Related item field value is not assigned to the field when the task is created, so pay attention using event receivers events ItemAdding and ItemAdded for related item field read, use ItemUpdated or ItemUpdating instead.
I wrote this little piece of code you can find in Example 08, which overwrites the task related field item with the first item of the workflow items list in a event receiver.
string jsonString = properties.ListItem[SPBuiltInFieldId.RelatedItems].ToString();
List relatedItems = RelatedItemFieldHelper.GetItems(jsonString);
RelatedItemFieldValue r = relatedItems[0];
SPList l = properties.Web.Lists.TryGetList("WorkflowsItemsList");
SPQuery query = new SPQuery();
query.RowLimit = 1;
query.Query = "";
SPListItem first = l.GetItems(query)[0];
int itemId = first.ID;
r.ItemId = itemId;

// Safety... never enough
this.EventFiringEnabled = false;
properties.ListItem[SPBuiltInFieldId.RelatedItems] = RelatedItemFieldHelper.GetItems(relatedItems);
properties.ListItem.SystemUpdate();
// Rollbacking safety setting
this.EventFiringEnabled = true;
And here's the demonstration i'm telling the truth :-)


 Even if i'm confident you know it, here's the link to VLibs code :-) .
written in: Milano, Italia

2 comments:

  1. Hi Valerio, you made my day. I had an issue where I'm using item event receivers on task list items and on the ItemAdded event, I'm overwriting the permissions of the current user and giving him read permissions on the task item. The problem is that "RelatedItems" field did not updated after that. Reading your blog pushed me to move my business to the ItemUpdated method with a little help from SharePoint which, I guess, is updating the task item after adding it (I'm sure thanks to the RelatedItems field) so I can rely on the ItemUpdated event knowing that it will be fired even when a task is created.

    ReplyDelete
  2. Hi Mohamed, you made my day, with this comment :-)
    Best regards and good work

    ReplyDelete

Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?