0

I have a website with Sitecore 10.1 and MVC. There is a requirement to get the page data and data of all components placed on the page in a single JSON object.

We are not using the Sitecore layout services.

Is it possible to get the page and it's component data through API in JSON format?

1 Answer 1

0

If you want to get the Item data in the form of JSON Object using API then you need to work with the ItemService using Sitecore.

You can use the ItemService in the following ways:

  • Directly with the ItemService RESTful API.

  • With the ItemService JavaScript library.

For example, if you want to get the item details then you can call it using this API request.

/sitecore/api/ssc/item/110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9

There are lots of examples provided in the Item Service and you can use whatever is required of you.

Here are the details for this.

https://doc.sitecore.com/xp/en/developers/90/sitecore-experience-manager/the-itemservice.html

And this will also help to get the details of parameters you can use in the call.

https://doc.sitecore.com/xp/en/developers/92/sitecore-experience-manager/the-restful-api-for-the-itemservice.html

Finally, if the above APIs are not able to give you the specific data that you need, then I would suggest you write the C# code. Like if you want to get the renderings on an item then you can write something like this.

string deviceName = "Default".ToLower();

var deviceItem = item.Database.Resources.Devices.GetAll().Where(d => d.Name.ToLower() == deviceName).First();
if (deviceItem != null)
{
    var renderings = item.Visualization.GetRenderings(deviceItem, false);
    
    if (renderings != null)
    {
        foreach (var renderingref in renderings)
        {
            // Get your data from renderingref, bind it in a class and convert that in JSON.
        }                   
    }
}   

And you can get the data you need and create your own JSON object.

5
  • Hi Sumit, Above examples will give me the item data and rendering information, however I want the output of renderings using api call. Once I will get the rendering output using api, I will convert that in JSON object.
    – Alok Kumar
    Nov 9 at 17:45
  • @AlokKumar I understand your concern, and here I have a suggestion for you to check the Get item including standard template fields. It will give you all the standard fields and there is a field on every item called Renderings and Final renderings where you can get the raw value of all the components associated with an item. Nov 10 at 3:23
  • Renderings and Final renderings actually will give the properties of rendering component like data-source, parameters, rendering id etc.. How I can get the values which component will return after execution thru API.
    – Alok Kumar
    Nov 10 at 16:54
  • If you analyze the Renderings and Final renderings fields and see how the raw value is being used, you will get the datasource id of the rendering. And you can pass this datasource ID in using the same item service API to get it's value. Refer this for more details. seanholmesby.com/… Nov 11 at 5:46
  • 1
    As per your statement, that how you will get the values which component will return after execution, so if you are getting any Sitecore Item using API, you will get the values set on that item including the field. Nov 11 at 5:47

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.