The other day I was exploring pipelines of the Sitecore Item Web API, and took note of the itemWebApiGetProperties pipeline. This pipeline adds information about an item in the response returned by the Sitecore Item Web API. You can find this pipeline at /configuration/sitecore/pipelines/itemWebApiGetProperties in \App_Config\Include\Sitecore.ItemWebApi.config.
The following properties are set for an item in the response via the lonely pipeline processor — /configuration/sitecore/pipelines/itemWebApiGetProperties/processor[@type=”Sitecore.ItemWebApi.Pipelines.GetProperties.GetProperties, Sitecore.ItemWebApi”] — that ships with the Sitecore Item Web API:
Here’s an example of what the properties set by the above pipeline processor look like in the response — I invoked a read request to the Sitecore Item Web API via a copy of the console application written by Kern Herskind Nightingale, Director of Technical Services at Sitecore UK:
You might be asking “how difficult would it be to add in my own properties?” It’s not difficult at all!
I whipped up the following itemWebApiGetProperties pipeline processor to show how one can add more properties for an item:
using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.ItemWebApi; using Sitecore.ItemWebApi.Pipelines.GetProperties; namespace Sitecore.Sandbox.ItemWebApi.Pipelines.GetProperties { public class GetEvenMoreProperties : GetPropertiesProcessor { public override void Process(GetPropertiesArgs arguments) { Assert.ArgumentNotNull(arguments, "arguments"); arguments.Properties.Add("ParentID", arguments.Item.ParentID.ToString()); arguments.Properties.Add("ChildrenCount", arguments.Item.Children.Count); arguments.Properties.Add("Level", arguments.Item.Axes.Level); arguments.Properties.Add("IsItemClone", arguments.Item.IsItemClone); arguments.Properties.Add("CreatedBy", arguments.Item["__Created by"]); arguments.Properties.Add("UpdatedBy", GetItemUpdatedBy(arguments.Item)); } private static Dynamic GetItemUpdatedBy(Item item) { Assert.ArgumentNotNull(item, "item"); string[] usernamePieces = item["__Updated by"].Split('\\'); Dynamic username = new Dynamic(); if (usernamePieces.Length > 1) { username["Domain"] = usernamePieces[0]; username["Username"] = usernamePieces[1]; } else if (usernamePieces.Length > 0) { username["Username"] = usernamePieces[0]; } return username; } } }
The ParentID, ChildrenCount, Level and IsItemClone properties are simply added to the properties SortedDictionary within the GetPropertiesArgs instance, and will be serialized as is.
For the UpdatedBy property, I decided to leverage the Sitecore.ItemWebApi.Dynamic class in order to have the username set in the “__Updated by” field be represented by a JSON object. This JSON object sets the domain and username — without the domain — into different JSON properties.
As a side note — when writing your own service code for the Sitecore Item Web API — I strongly recommend using instances of the Sitecore.ItemWebApi.Dynamic class — or something similar — for complex objects. Developers writing code to consume your JSON will thank you many times for it. 🙂
I registered my new processor to the itemWebApiGetProperties pipeline in my Sitecore instance’s \App_Config\Include\Sitecore.ItemWebApi.config:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <!-- there's stuff here --> <itemWebApiGetProperties> <processor type="Sitecore.ItemWebApi.Pipelines.GetProperties.GetProperties, Sitecore.ItemWebApi" /> <processor type="Sitecore.Sandbox.ItemWebApi.Pipelines.GetProperties.GetEvenMoreProperties, Sitecore.Sandbox" /> </itemWebApiGetProperties> <!-- there's stuff here as well --> </sitecore> </configuration>
Let’s take this for a spin.
I ran the console application again to see what the response now looks like:
As you can see, our additional properties are now included in the response.
If you can think of other item properties that would be useful for Sitecore Item Web API client applications, please share in a comment.
Until next time, have a Sitecorelicious day!
Reblogged this on Sutoprise Avenue, A SutoCom Source.
[…] response indicating whether the Item has content in the requested language (check out my previous post on adding additional properties to Sitecore Item Web API responses for more information on this […]