Sitecore OData Item Api extensions for reference type fields

 Sitecore OData Item Api provides readonly access to Sitecore items. The OData Item Api is particularly useful when you need to pull some data from Sitecore to show on a non-Sitecore websites. In my case, I needed to pull a list of current job opening in my organization to show them on our company's non-Sitecore website's careers page.

For more information on OData Item Api and how to query the data, check the Sitecore documentation page. One thing that particularly bothered me when using OData Item Api service to get an item and its field values using a query like below is, the lack of support for getting the link field target URL in one query.

/sitecore/api/ssc/aggregate/content/Items('{F7SDEBB9-6AB1-4ACC-A790-FE627DD18722}')?$expand=FieldValues&sc_apikey={ADGT918E-8F0F-4F7E-8C77-DEE3F4F2E1Y1}

When you run the above query against an item with General link field, the output for the field values looks something like this

As you can see, the link field value is just the raw value stored in the field. To get the target item URL the link is pointing to, you need to extract the target item id and make another OData query to the Api. It is not practical to make one call per item to get the target URL when you are fetching multiple items in one query. 

Luckily, we can easily extend the OData Item Api to include our own fields in the output. This can be done by overwriting the ItemDataModelFactory used by the Item Api to resolve the item contents with our own ItemDataModelFactory implementation. To do this, we need to create a custom ItemDataModelFactory that inherits from Sitecore.Content.Services.Items.OData.ItemDataModelFactory class and overriding the GetFieldValues method.

Here is how the custom ItemDataModelFactory implementation that extends General Link field output to include the Url and few other fields looks like

We are overriding both GetFields and GetFieldValues methods. While GetFields is used by /sitecore/api/ssc/aggregate/content/Items('{ItemID}')?$expand=Fields query, GetFieldValues is used by /sitecore/api/ssc/aggregate/content/Items('{ItemID}')?$expand=FieldValues query. 

To register this custom ItemDataModelFactory implementation we need to write a custom configurator class for OData Item Api service.

Now that our custom configurator is ready, we just need to override the default implementation using a config patch.

With the custom implementation in place we can now see the additional fields we added for General Link fields in the output.

We can also add additional fields for other reference type fields like Multi List, Droplink, Image field etc. by adding the properties and transforms for those fields in CreateFieldModel and CreateFieldValuesModel methods respectively.

Comments

Popular posts from this blog

SXA Scriban extension to get link field target URL

View and download Sitecore log files using Log Viewer

How to ace your Sitecore .Net Developer 10 Certification Exam