1

When extending JssGetLayoutServiceContextProcessor, is there a way to call ProcessItem just like in Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver?

public class SampleContextExtension : Sitecore.JavaScriptServices.ViewEngine.LayoutService.Pipelines.GetLayoutServiceContext.JssGetLayoutServiceContextProcessor
   {
       public SampleContextExtension(IConfigurationResolver configurationResolver) : base(configurationResolver)
       {
       }

       protected override void DoProcess(GetLayoutServiceContextArgs args, AppConfiguration application)
       {
           var item = Factory.GetDatabase("master").GetItem(new ID("11112222-4444-5555-6666-3333AAAA2222"));
           // Use ProcessItems here?
           args.ContextData.Add("contextItems", ProcessItems(item.Children, rendering, renderingConfig));
       }
   } 
3

Since you are extending the JssGetLayoutServiceContextProcessor, you will not have access to the ProcessItems method from the RenderingContentsResolver.

One thing you can do is to implement it within your SampleContextExtension class. The code for the ProcessItems is as follows:

protected virtual JArray ProcessItems(IEnumerable<Item> items, Sitecore.Mvc.Presentation.Rendering rendering, IRenderingConfiguration renderingConfig)
{
  JArray jarray = new JArray();
  foreach (Item obj in items)
  {
    JObject jobject1 = this.ProcessItem(obj, rendering, renderingConfig);
    JObject jobject2 = new JObject()
    {
      ["id"] = (JToken) obj.ID.Guid.ToString(),
      ["name"] = (JToken) obj.Name,
      ["displayName"] = (JToken) obj.DisplayName,
      ["fields"] = (JToken) jobject1
    };
    jarray.Add((JToken) jobject2);
  }
  return jarray;
}

protected virtual JObject ProcessItem(Item item, Sitecore.Mvc.Presentation.Rendering rendering, IRenderingConfiguration renderingConfig)
{
    Assert.ArgumentNotNull((object) item, nameof (item));
    using (new SettingsSwitcher("Media.AlwaysIncludeServerUrl", this.IncludeServerUrlInMediaUrls.ToString()))
    {
        return JObject.Parse(renderingConfig.ItemSerializer.Serialize(item));
    }
}

So, from the ProcessItems(), it calls the ProcessItem() method

Note, you may also implement it in your own ways by creating an object for the properties Id, Name, DisplayName. It is only for the fields that uses the ProcessItem method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.