Accessing Sitecore content from Commerce engine apps
While working on Sitecore Commerce project recently I came upon very interesting API that is exposed in Commerce Engine app.
As you know during its startup Sitecore Commerce apps (authoring, shops, minions, etc.) are connecting to Sitecore instance to load its own configuration.
The credentials and connection to Sitecore CM instance is taken from PlugIn.Content.PolicySet-1.0.0.json
Luckily the class that is making the connection to Sitecore is public and we can easily use it in our commerce apps:
By default SitecoreConnectionManager provides following APIs from Sitecore:
- GetItemByIdAsync
- GetItemByPathAsync
- GetItemsByPathAsync
- GetItemChildrenAsync
- SearchMediaItemsAsync
- GetLanguage
- ProcessRequestAsync
What if something is missing?
Here is when ProcessRequestAsync could be very handy — we could expose an API from the Sitecore side using ServicesApiController and make a request to it from Sitecore Commerce.
A great example how to use Sitecore API controllers could be found here:
https://laubplusco.net/implementing-webapi-service-using-servicesapicontroller-sitecore-8/
The positive part about using Sitecore API controller and SitecoreConnectionManager is that the security aspect is already taken care of. Sitecore by default would authenticate the Sitecore instance with provided credentials, so the API wouldn’t be exposed to everyone.