Access content from Sitecore XMCloud with Asp.net Rendering Host

    

    

If we need to use the Asp.net RenderingHost instead of Next.js or React.js and to access the content from XMCloud. 

There are 2 ways to access the content from XM cloud. 

  • Using Edge GraphQL(published content)
  • Without Edge (from master database layoutservice)

Steps for creating asp.net solution for xmcloud

  • Create an asp.net renderinghost project manually and update all necessary configuration or use the following command to get the getting started template for creating the project
  • dotnet new -i Sitecore.DevEx.Templates --nuget-source https://sitecore.myget.org/F/sc-packages/api/v3/index.json
  • Then Run following command to create the project 
  • dotnet new sitecore.aspnet.gettingstarted -n MyXmCloud

Note:

The above command will create project with following structure, from here use the RenderingHost project to access the XM cloud content and design the pages accordingly.

 

Steps for accessing the content from XM cloud using without Edge GraphQL

  • Open Appesttings.config file and configure the following settings.

"LayoutService": {

    "Handler": {

      "Name": "jss-endpoint",

      "Uri": "https://xmcloudsitecoredomainname/sitecore/api/layout/render/jss",

      "RequestDefaults": {

        "sc_apikey": "{8C9133A0-8640-43E5-8EFB-864ACD1A7F4C}", // api key

        "sc_site": "myfirstsite" //name of the site from which content to be retrieved

      }

    }

  },

  "RenderingEngine": {

    "ExperienceEditor": {

      "Endpoint": "/jss-render"

    }

  },

  "JSS_EDITING_SECRET": "xxxxxxxxxxxxxxxx", //This setting used for experience editor and should match sitecore setting JavaScriptServices.ViewEngine.Http.JssEditingSecret

  "Analytics": {

    "SitecoreInstanceUri": "https://xmcloudsitecoredomainname/"

  }

  • Create component name RichText by creating model in the project with the same name of the rendering created in sitecore XMCloud.

using Sitecore.LayoutService.Client.Response.Model.Fields;

namespace MyProject.Models

{

    public class RichText

    {

        public RichTextField Text { get; set; }

    }

}

  • Create the necessary view(RichText.cshtml) file to bind the model content coming from layoutservice

@model RichText

@{

 

  <div class="col-sm">

    <div class="contentDescription">

     MyRichtext: <sc-text asp-for="Text" />

    </div>

  </div>

}

 

  • Map this new model and view as component in startup.cs as below

                services.AddSitecoreRenderingEngine(options =>

                {

                    options                      

                        .AddModelBoundView<ContentBlock>("ContentBlock")                     

                        .AddModelBoundView<RichText>("Richtext") //new component added

                        .AddDefaultPartialView("_ComponentNotFound");

                })

 

Steps for accessing the content from XM cloud using Edge

  • Access the content using Preview edge endpoint /sitecore/api/graph/edge 
  • Access the content using live edge endpoint  /api/graphql/v1
  • open appsettings.development.json and update the following settings   

          Sitecore: {
            "InstanceUri": "https://domainname/",
            "LayoutServicePath": "/sitecore/api/graph/edge", //Replace this with live edge endpoint
            "DefaultSiteName": "MySite",
            "ExperienceEdgeToken": "{8C9133A0-8640-43E5-8EFB-864ACD1A7F4C}",//This is point to sitecore api key for                 preview,replace this with edge token for live edge
            "MvpDirectoryGraphQLQueryPageSize": 1000,
            "MvpDirectoryGraphQLQueryCacheTimeout": 1
        }

 


Comments

Popular posts from this blog

Custom Item Url and resolving the item in Sitecore - Buckets

Fixing Sitecore Buckets folder path - Items created after 12 AM server time zone

Sitecore Search - API Crawler with Edge Pagination