SXC9 - Reading Sitecore XP content items in Commerce Engine Code

Sitecore can access Commerce data via Commerce Connect or via Commerce APIs, but sometimes we need to go the other way around and read Sitecore content items from Commerce Engine (CE) - how can this be done? Not sure if this is covered in Commerce documentation, we resorted to decompiling CE DLLs and figured this actually is quite simple. This post will provide step by step instructions for how to retrieve Sitecore content item and read its properties.

Commerce Engine configuration policy for Sitecore XP access

Sitecore Commerce install script creates PlugIn.Content.PolicySet-1.0.0.json file under wwwroot\[Your Commerce Environment]\wwwroot\data\Environments for each one of the four environments, created during the install. This configuration policy, among other things, specifies the hostname, login user and password for Commerce Engine to be able to access Sitecore XP content items. Note that different CE instances can be configured to communicate with different Sitecore instances, e.g. “Commerce Authoring” engine might be configured to access Content Management (CM) server and “Commerce Shops” might point to Content Delivery (CD). Commerce Engine code uses this policy when it needs to communicate with Sitecore XP.

Accessing Sitecore Content Items from Commerce Engine Code

GetItemByIdPipeline is what does all the work – we’ll need to instantiate it, set up its request parameters and then run it:

  1. Instantiate GetItemByIdPipeline using Dependency Injection, simply by adding it to the list of parameters of calling class in Commerce Engine code

    public class SomeCommerceEngineBlock : PipelineBlock<CommerceEntity, CommerceEntity, CommercePipelineExecutionContext>
        IGetItemByIdPipeline _getItemByIdPipeline;
        public SomeCommerceEngineBlock(IGetItemByIdPipeline getItemByIdPipeline) : base((string)null)
            _getItemByIdPipeline = getItemByIdPipeline;
        //Insert your code here...
  2. Create a request argument with desired language and Siteore item ID (or Sitecore path to content item)

    var language = context.CommerceContext.CurrentLanguage();
    if (language == null)
        throw new ApplicationException("Current Language canot be idetified - check language configuration in Commerce Control Panel");
    var itemModelArgument = new ItemModelArgument(itemId)
        Language = language,
  3. Execute pipeline to retrieve content item from Sitecore XP

    var itemModel = await getItemByIdPipeline.Run(itemModelArgument, context);
  4. itemModel will come back empty if Sitecore content item is not found. Otherwise you can read it as string dictionary and, if needed, iterate through its fields with code like this

    if (itemModel == null || itemModel.Keys.Count == 0)
        //Item not found
        return null;
    foreach (var key in itemModel.Keys)
        var sitecoreFieldName = key;
        var sitecoreFieldValue = itemModel[key];
        //Use item data as needed...