Alexa and Sitecore Integration using OData and XConnect

Introduction

In this series of blog posts - we will take a whirlwind tour of how to create an Alexa skill to integrate with Sitecore. The skill will connect with Sitecore's OData Item Service to query content. In later posts we will also be recording Contacts and Events with XConnect. In this post - we will start with the basics.

The Proof of Concept

In true Blue Peter style - here's one I made earlier. I have created items representing dogs based on the Dog template in Sitecore. The Dog template has fields for description, celebrity (famous example) and personality. I will be querying by these fields.


Template Fields

Now - querying content via Alexa (sorry about the shaky video!)





Enabling the OData Item Service

With the release of Sitecore 9 - there is now a new OData Item Service (OIS) that allows us to query and retrieve content using the OData protocol. The OData Item Service is part of the Sitecore.Services.Client framework that enables developers to expose content and entities via APIs. It is just one of the varied ways in which Sitecore can be made "headless". It is also important to remember that OIS is read-only so you cannot import or update items as you can through other APIs.

In order to use OIS - we first need to enable access. This is done by setting up an OData API Key item in the Core database under /sitecore/system/settings/services/API Keys.




The settings that you need to specify are:
  • Database - this is the content database from which content will be queried and retrieved ie.. master or web.
  • CORS Origin - this is a semicolon delimited list of CORS domains that are allowed to access your content. I have specified * but you may choose to be more selective.
  • Allowed Controlers - this is a semicolon delimited list of controllers that are allowed to use the API Key. For this example I have just used the built-in Sitecore.Content.Services.Controllers.ItemsController. However, this setting does seem to imply that you can write your own OData compliant controllers.
  • Impersonation User - this is effectively the user context under which content will be queried and retrieved. OIS can be accessed anonymously using the account associated with the Sitecore.Services.AnonymousUser configuration setting. However, we can override this setting by specifying the account we wish to use in the Impersonation User field.
That is literally all you need to get started with OIS. Before you can start querying - ensure that your content is published (if using web db) and that SOLR is running and your indexes are up-to-date.

OData - where for art thou!?

The OData requests we will be making are all GET requests where we call the Item controller and pass some query parameters as well as the API key. It is important to note that the API key can either be passed through as a query string parameter or as a HTTP header. The format of the requests is as follows:

https://[service root]/[resource path]?[query options]

In our examples the service root is http://sc9xp0.sc/sitecore/api/ssc/aggregate/content/ and our resource path starts with /Items as we are interested in querying and retrieving items. The query options we use depend on what we want to get back. Below are some examples of OData queries.

Get the Home item:

http://sc9xp0.sc/sitecore/api/ssc/aggregate/content/Items('{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}')?sc_apikey={28EAA678-F822-4341-BC75-C1CFB0A95B92}

Get all items that are derived from the Dog template:

http://sc9xp0.sc/sitecore/api/ssc/aggregate/content/Items?sc_apikey={28EAA678-F822-4341-BC75-C1CFB0A95B92}&$filter=(TemplateName eq 'Dog')

Get a Dog item with a Celebrity field value of Marmaduke:

http://sc9xp0.sc/sitecore/api/ssc/aggregate/content/Items?sc_apikey={28EAA678-F822-4341-BC75-C1CFB0A95B92}&$filter=TemplateName eq 'Dog' and Fields/any(f: f/Name eq 'Celebrity' and f/Value eq 'Marmaduke')

As you can see it's fairly easy to understand the queries being carried out above. When you execute such queries you will get a JSON response back like below with zero or more items.

{
    "@odata.context": "http://sc9xp0.sc/sitecore/api/ssc/aggregate/content/$metadata#Items",
    "value": [
        {
            "Name": "Great Dane",
            "DisplayName": "",
            "Path": "/sitecore/content/Home/Dogs/Great Dane",
            "Language": "en",
            "Version": 1,
            "IsLatestVersion": true,
            "TemplateId": "01754839-d487-4135-8db3-c3357790506f",
            "TemplateName": "Dog",
            "Created": "2017-11-01T18:51:21Z",
            "Updated": "2017-11-01T18:55:00Z",
            "ParentId": "8939556c-4960-4283-b583-b155b4b5c67b",
            "Id": "14cc922e-93f0-41fe-91d7-d245442c9159",
            "Url": "http://sc9xp0.sc/sitecore/api/ssc/aggregate/content/Items('14cc922e-93f0-41fe-91d7-d245442c9159')"
        }
    ]
}

This data can then be consumed by a calling application - be it another controller or client application including a Alexa skill. The specifics Alexa skill will follow in another blog post. Below is an image of an actual request / response between Alexa and OIS.

Thanks for reading - I hope you found this technique useful. Feel free share this article and leave feedback.



Comments

Popular posts from this blog

Alexa and Sitecore Integration - Part 3

Getting Up and Running with Sitecore 9

Introducing Helix DNA