Created
November 20, 2017 18:20
-
-
Save alexshyba/b3d2fe8187cd2f078ce8560f961ec788 to your computer and use it in GitHub Desktop.
Sample IRenderingContentsResolver implementations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json.Linq; | |
using Sitecore; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.LayoutService.Configuration; | |
using Sitecore.LayoutService.ItemRendering; | |
using Sitecore.Links; | |
using Sitecore.Mvc.Presentation; | |
namespace Nomad.Jss.CodeFirst.ItemRendering | |
{ | |
public class ContextItemChildrenRenderingContentsResolver : IRenderingContentsResolver | |
{ | |
private UrlOptions _urlOptions; | |
public virtual UrlOptions UrlOptions | |
{ | |
get | |
{ | |
if (_urlOptions != null) | |
{ | |
return _urlOptions; | |
} | |
_urlOptions = LinkManager.GetDefaultUrlOptions(); | |
_urlOptions.LanguageEmbedding = LanguageEmbedding.Never; | |
_urlOptions.AlwaysIncludeServerUrl = false; | |
_urlOptions.SiteResolving = false; | |
return _urlOptions; | |
} | |
set => _urlOptions = value; | |
} | |
public virtual object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig) | |
{ | |
Assert.ArgumentNotNull(rendering, "rendering"); | |
Assert.ArgumentNotNull(renderingConfig, "renderingConfig"); | |
var contextItem = Context.Item; | |
var itemArr = new JArray(); | |
using (new Sitecore.Configuration.SettingsSwitcher("Media.AlwaysIncludeServerUrl", "true")) | |
{ | |
foreach (Item item in contextItem.GetChildren()) | |
{ | |
var itemJson = JObject.Parse(renderingConfig.ItemSerializer.Serialize(item)); | |
itemJson["ItemUrl"] = LinkManager.GetItemUrl(item, UrlOptions); | |
var resultJson = new JObject {["fields"] = itemJson}; | |
itemArr.Add(resultJson); | |
} | |
} | |
var contents = new JObject | |
{ | |
["items"] = itemArr | |
}; | |
return contents; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json.Linq; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.LayoutService.Configuration; | |
using Sitecore.LayoutService.ItemRendering; | |
using Sitecore.Mvc.Presentation; | |
namespace Nomad.Jss.CodeFirst.ItemRendering | |
{ | |
public class DatasourceChildItemsRenderingContentsResolver : IRenderingContentsResolver | |
{ | |
public object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig) | |
{ | |
Assert.ArgumentNotNull(rendering, "rendering"); | |
Assert.ArgumentNotNull(renderingConfig, "renderingConfig"); | |
//don't use Rendering.Item, as we don't want the context item | |
var datasource = !string.IsNullOrEmpty(rendering.DataSource) | |
? rendering.RenderingItem?.Database.GetItem(rendering.DataSource) | |
: null; | |
if (datasource == null || !datasource.HasChildren) | |
{ | |
return null; | |
} | |
var itemArr = new JArray(); | |
using (new Sitecore.Configuration.SettingsSwitcher("Media.AlwaysIncludeServerUrl", "true")) | |
{ | |
foreach (Item item in datasource.GetChildren()) | |
{ | |
var itemJson = JObject.Parse(renderingConfig.ItemSerializer.Serialize(item)); | |
var resultJson = new JObject { ["fields"] = itemJson }; | |
itemArr.Add(resultJson); | |
} | |
} | |
var contents = new JObject | |
{ | |
["items"] = itemArr | |
}; | |
return contents; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment