Related items field – how to access it programmatically by code?

When paying with new Workflow tasks I have noticed that they now do contain the new field of type “related items”.

This is an awesome field that allows you to add links to any items from your site collection.

By default this field exists in a new content type called Workflow Task (SharePoint 2013).

 

So what happens when a SharePoint 2013 workflow creates a task for an item? It fills a link to an original item that workflow has to do with in this field.

 

Her is how it looks like.

 

If you press Add related item, then

You see a nice dialog that allows you to open any item from your site collection.

 

 

 

Once a customer asked me to do the following logic. Once a task has been updated by someone, add comments and the guy who actually implemented the task to the original item.

So I came to a problem – how do I actually read the related items and find out what is inside?

 

When I looked at the fields I figured out it is actually a multiline text field

 

So when I wrote a short program that actually gets the value and I was surprised to see it is actually JSON!

 

 

Or in JSON.

 

 

So basically you need to add a simple parser that will read the values.

 

I have tried a parser which I found out in ASP.NET MVC4 but apparently it needed a couple of DLLS to be installed which we could not figure out, so I have used a NewtonSoft JSON which was added with a simple nuget package.

 

So the code of reading the values from related items is

 

using (ClientContext ctx = GetAuthenticatedContext())

{

TraceHelper.TraceInformation(ConsoleColor.Magenta, “reading related items”);

 

ctx.Load(ctx.Web, x=>x.ServerRelativeUrl);

ctx.ExecuteQuery();

 

var workflowTasksListUrl = UrlUtility.CombineUrl(ctx.Web.ServerRelativeUrl, Lists.WorkflowTasks.GetListUrl());

var workflowTasksLIst = ctx.Web.GetList(workflowTasksListUrl);

 

// this is just for demo, this is not the best code to work with sharpeoint items

var items = workflowTasksLIst.GetItems(

new CamlQuery()

);

 

ctx.Load(items);

 

ctx.ExecuteQuery();

 

string relatedItemsString = (string)items[0][BuiltInInternalFieldNames.RelatedItems];

 

dynamic decodedRelatedItems = JsonConvert.DeserializeObject(relatedItemsString);

foreach (var item in decodedRelatedItems)

{

int itemId = int.Parse(item.ItemId.ToString());

var listId = new Guid(string.Format(“{{{0}}}”, item.ListId.ToString()));

var webId = new Guid(string.Format(“{{{0}}}”, item.WebId.ToString()));

 

Console.WriteLine(“Found an item from web {0}, list {1}, itemid:{2}”,webId,listId,itemId);

 

}

 

And can be found in a demo app called “DemoAp.Reader”.

 

Before you can use it run DemoApp.AssetBuilder that uses spmeta2 for provisioning and builds a structure. Sample app is supposed to run over Office 365.

 

Source code can be found at

https://github.com/maratbakirov/2015_03_related_fieldsDemo

Comments

  • Anonymous
    March 22, 2015
    That will definitely come in handy. Thanks!

  • Anonymous
    March 22, 2015
    Thanks Russel.