Sitecore Item Web API introduces some new pipelines declared in Sitecore.ItemWebAPI.config
The key pipeline you are looking for is itemWebApiRead
. This references the type Sitecore.ItemWebApi.Pipelines.Read.GetResult, Sitecore.ItemWebApi
which executes the method GetFieldInfo
. This method actually sets the values returned for each field in the response.
As the method is virtual
we can simply write our own class which inherits from Sitecore.ItemWebApi.Pipelines.Read.GetResult
and implement our own GetFieldInfo
method
Implementation
using Sitecore.Data.Fields;
namespace WebApi
{
public class GetResult: Sitecore.ItemWebApi.Pipelines.Read.GetResult
{
protected override Sitecore.ItemWebApi.Dynamic GetFieldInfo(Field field)
{
var fieldInfo = base.GetFieldInfo(field);
if (field.TypeKey == "image")
{
var imageField = (ImageField) field;
fieldInfo.Add("Alt", imageField.Alt);
}
return fieldInfo;
}
}
}
As you can see, all we need to do is check the field.TypeKey
to see if it is an image. If so, we cast to and ImageField
and then add in the value of the Alt
property to our fieldInfo
object.
Result
"{4A0E7425-5927-42D6-9438-A17E875DE50A}": {
"Name": "Image",
"Type": "Image",
"Value": "<image mediaid=\"{00065A73-4859-4CF3-9F51-4D01D39CAD75}\" mediapath=\"/Images/128\" src=\"~/media/00065A7348594CF39F514D01D39CAD75.ashx\" />",
"Alt": "sitecore logo"
}
If you need to add further customisations for other field types you can always add a switch statement based off the TypeKey
.