1

I am using Sitecore with SXA. I have a requirement that I want to render one rendering variant field with data which is coming from live API e.g data from some stock exchange which varies all the time. Generally rendering variant fields are mapped with template fields but in this case I think Model type field can be used to get data from API instead of Sitecore Item field.

Can somebody please confirm how can I use this field, also how to pass value in property field.

1

Model

The Model type can indeed be used to display data from a custom model object - if you can fill that object with your API data that is certainly an option. I wrote a small article on this a few years ago: https://ggullentops.blogspot.com/2019/03/external-model-data-SXA-variant-Sitecore.html

Suppose the model of your component looks a bit like this:

public class CustomModel: VariantsRenderingModel
{
  public string CustomProperty { get; set; }
  public CustomClass CustomClassProperty { get; set; }
}

then you can use the properties with a Model variant like this: enter image description here or enter image description here

Make sure the properties are filled of course.

Scriban

Depending on your SXA version, Scriban could also be an option. In a Scriban template you can also use o_model to access object data just as the model type. But if it gets a bit more complicated you could also create your own Scriban extension - which is actually not that hard to do (https://ggullentops.blogspot.com/2019/11/custom-sxa-scriban-extensions.html).

You would need a context function - something like this:

using Scriban.Runtime;
using Sitecore.XA.Foundation.Abstractions;
using Sitecore.XA.Foundation.Scriban.Pipelines.GenerateScribanContext;

public class ScribanApiData : IGenerateScribanContextProcessor
{
    private readonly IContext context;

    public ScribanApiData(IContext context)
    {
        this.context = context;
    }

    public void Process(GenerateScribanContextPipelineArgs args)
    {
        var apiData = new ApiData(GetApiData);
        args.GlobalScriptObject.Import("sc_apidata", (Delegate)apiData);
    }

    public string GetApiData(string key)
    {
        //return your api data;
    }

    private delegate string ApiData(string key);
}

Configure it like this:

<sitecore>
  <pipelines>
    <generateScribanContext>
      <processor type="Sxa93.ScribanApiData, Sxa93" resolve="true" />
    </generateScribanContext>
  </pipelines>
</sitecore>

and use it like {{ sc_apidata "Key" }}.

In this example a "key" is passed to the class fetching the api. That can be extended or skipped if needed.

You can also return all kinds of data - if you need to return a custom object though it becomes a bit more difficult (that might be another topic).

2
  • 1
    As it stands, this is a link-only answer. Please add the relevant information so that the answer is clearly visible without having to leave the SSE site.
    – Richard Seal
    Aug 24 at 20:54
  • 1
    Added the information in the post..
    – Gatogordo
    Aug 25 at 9:39
0

You could write a Scriban extension which would go as below:

public class MyApiExtension : IGenerateScribanContextProcessor
    {
        private readonly IContext context;
        private delegate MyModel GetContent(string modelId);

        public MyApiExtension (IContext context)
        {
            this.context = context;
        }

        public void Process(GenerateScribanContextPipelineArgs args)
        {
            var getApiContent = new GetContent(GetApiContent);
            args.GlobalScriptObject.Import("sc_get_api_content", getApiContent);
        }

        public MyModel GetApiContent(string modelId)
        {
            var myModel = new MyModel();
            // Perform API actions and populate the myModel object
            return myModel;    
        }
}

The config file for patching this would be as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
          <generateScribanContext>
            <processor type="Foundation.SitecoreExtensions.MyApiExtension, Foundation.SitecoreExtensions" resolve="true" />
                </generateScribanContext>
         </pipelines>
    </sitecore>
</configuration>

Accessing it in Scriban would be as below:

{{$myObj = sc_get_api_content i_item.ModelId}}
<h2>{{$myObj.name}}</h2>
2
  • Did you test the fact that you can return a custom object and access it's properties? Just asking because I think you can't... (well, not without adding that object to Scriban as well)
    – Gatogordo
    Aug 25 at 9:41
  • @Gatogordo We have something like this working in a solution in Sitecore 10.0. It definitely works. Aug 25 at 13:46

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.