How to get data from sitecore cms item from a Sitecore Experience Commerce 9 Plugin

You may have need to get data from an item in Sitecore CMS while writing a plugin.

This blog is about a way you can do that.

First make sure you add or have added the right version of Sitecore.Commerce.Plugin.Management NuGet Package.

The righ version for Sitecore Experience Commerce 9.2 is 4.0.26, for 9.1 is 3.0.14, for version 9.0.3 is 2.4.9

Add NuGet package


Now, you can go to the constructor in your plugin block or your controller and inject the pipeline for getting item by path or for getting item by Id

To get Sitecore item by path, inject:

IGetItemByPathPipeline getItemByPathPipeline

To get Sitecore item by Id, inject: 

IGetItemByIdPipeline getItemByIdPipeline 

Inject pipelines in constructor


Say we want to get the default Home item in Sitecore and use its fields.

The path in Sitecore is "/sitecore/content/Home"

The Guid in Sitecore is "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"

The fields on the Home item are:


Inject pipelines in constructor


The code below with comments show how you would go about it and access the sitecore fields.

        public async Task<IActionResult> SampleFeatureCommand([FromBody] ODataActionParameters value)

            // Prepare item path your argument and set the language you want the returned item in.
            var sitecoreItemPathArgument = new ItemModelArgument("/sitecore/content/Home"){Language = CurrentContext.CurrentLanguage()};

            // Get the item from Sitecore by running the pipeline
            var itemFromSitecore = _getItemByPathPipeline.Run(sitecoreItemPathArgument, CurrentContext.PipelineContextOptions).Result;

            if (itemFromSitecore == null) return null;

            // prepare the properties model that will be used to transplate the item response from Sitecore
            var scItemProperties = new PropertiesModel
                Name = $"SitecoreItem_{itemFromSitecore["ItemID"] as string}"

            // Loop through the item response from Sitecore and use the value to set properties on scItemProperties 
            foreach (var keyValuePair in itemFromSitecore)
                if (keyValuePair.Value is string)
                    scItemProperties.SetPropertyValue(keyValuePair.Key, (string)keyValuePair.Value);
                    scItemProperties.SetPropertyValue(keyValuePair.Key, keyValuePair.Value);
            // You are now set to use the Sitecore fields of that item in your code, simply get them as shown below.
            // Remember to cast them to the appropriate types and do your null checks.
            var title = scItemProperties?.GetPropertyValue("Title");
            var text = scItemProperties?.GetPropertyValue("Text");

            await Task.Delay(0);

            return new ObjectResult(new { Title = title, Text = text });

If you want to use the item guid instead of the item path with the guid, and to get the item from Sitecore use "_getItemByIdPipeline" pipeline. 

var itemFromSitecore = _getItemByIdPipeline.Run(sitecoreItemPathArgument, CurrentContext.PipelineContextOptions).Result;  

Happy coding.

If you have any questions or require any help with a plugin you are developing, simply reach out to us at Xcentium and we will be happy to help.