The other day I had read a forum thread on SDN where the poster had asked whether one could determine if content returned from the Sitecore Item Web API for an Item was the actual content for the Item in the requested language.
I was intrigued by this question because I would have assumed that no results would be returned for the Item when it does not have content in the requested language but that is not the case: I had replicated what the poster had seen.
As a workaround, I built the following class to serve as an <itemWebApiGetProperties> pipeline processor which sets a property in the 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 topic):
using System.Collections.Generic; using System.Linq; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.Globalization; using Sitecore.ItemWebApi.Pipelines.GetProperties; namespace Sitecore.Sandbox.ItemWebApi.Pipelines.GetProperties { public class SetHasContentInLanguageProperty : GetPropertiesProcessor { public override void Process(GetPropertiesArgs arguments) { Assert.ArgumentNotNull(arguments, "arguments"); Assert.ArgumentNotNull(arguments.Item, "arguments.Item"); arguments.Properties.Add("HasContentInLanguage", IsLanguageInCollection(arguments.Item.Languages, arguments.Item.Language)); } private static bool IsLanguageInCollection(IEnumerable<Language> languages, Language language) { Assert.ArgumentNotNull(languages, "languages"); Assert.ArgumentNotNull(language, "language"); return languages.Any(lang => lang == language); } } }
The code in the above class checks to see if the Item has content in the requested language — the latter is set in the Language property of the Item instance, and the Languages property contains a list of all languages it has content for.
I then added the above pipeline processor via the following configuration file:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <itemWebApiGetProperties> <processor patch:after="processor[@type='Sitecore.ItemWebApi.Pipelines.GetProperties.GetProperties, Sitecore.ItemWebApi']" type="Sitecore.Sandbox.ItemWebApi.Pipelines.GetProperties.SetHasContentInLanguageProperty, Sitecore.Sandbox" /> </itemWebApiGetProperties> </pipelines> </sitecore> </configuration>
Let’s see how this works!
I first created an Item for testing:
This Item only has content in English:
I then toggled my Sitecore Item Web API configuration to allow for anonymous access so that I can make requests in my browser, and made a request for the test Item in English:
The Item does have content in English, and this is denoted by the ‘HasContentInLanguage’ property.
I then made a request for the Item in French:
As expected, the ‘HasContentInLanguage’ is false since the Item does not have content in French.
If you have any questions or thoughts on this, please drop a comment.
Hey Mike,
It seems this only works if the Language isn’t present in System Languages. For example if you have en-US and en-CA in System/Languages IsLanguageInCollection will always return true. Now if say you made a Item Web API Request using fr-CA which isn’t present in System/Languages then IsLanguageInCollection would return false.
Instead you need to also check if the Item.Versions.Count > 0 to check if there is a version of the item in the current language.
Thanks for that!
Not a problem, I would like to come up with a cleaner approach to exclude these items from being sent. Client side I just exclude the items where “HasContentInLanguage” is false.