Setup and Use Sitecore CMP Connector. Part 1
Tags: sitecore, cmp, xmcloud
Published: 2024-06-06 19:16:22

That sure is a nice Sitecore Content Hub you've got there. It is a good bet that you would like to use some of that content on XM Cloud. So lets do that.

This might get a bit long. So, briefly, here is what I am going to be describing how to do:

- Setup an Action and Trigger inside of Content Hub
- Setup the CMP connector inside of your XM Cloud application
- Setup data mappings for the content hub items.
- List some issues I had along the way and how they were overcome.

&nbsp; &nbsp;
Assumptions:

- You have access to create Triggers and Actions in Content Hub.
- The content item you need to sync has already been created. I will show off what the sample one looks like, but I will not go into its creation.
- You have access to add variables to your Sitecore XM Cloud project.
- You already setup an Oauth connection string setup in Content Hub that is being used to connect to the DAM. We will reuse this for the cmp connection, but these can be different if you have difference security requirements.
 
&nbsp; &nbsp;
## Content Hub Actions and Triggers
&nbsp; &nbsp; &nbsp;
---
### Create an action
Actions are the way we communicate between the two systems and triggers are ways to cause the action to fire.

The action we are going to want to setup is a [service bus action](https://doc.sitecore.com/xp/en/developers/connect-for-ch/50/connect-for-content-hub/create-a-sitecore-content-hub-action.html). There are two types supported.  

- <u>Azure Service Bus</u> - An azure service bus setup and maintained by you. So some of this setup can be useful for any other downstream application that might want to use the date being emitted by Content Hub.
- <u>M Azure Service Bus</u> - A service bus topic setup and maintained by Sitecore.

For this we are going to be using the 'M Azure Service Bus' because I don't want to have to setup and maintain anything myself. When you add a new action select the Type "M Azure Service Bus" and choose "Topic" as the Destination Type.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h50vx85bw36kiewsvqbh.png) &nbsp;


Make sure to copy the values that are generated in "Hub In" and "Hub Out". you will be using these later to setup the connection in XM Cloud.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tgxiy3f8ft9honux8pa.png) &nbsp;

Now we have a way to send the data, we need a way to trigger the information exchange to happen.

&nbsp; &nbsp; &nbsp;
---
### Create a trigger

We now need to tell Content Hub to actually send the message. For this instance we are publishing out a simple new article which has already been defined as a content item. The news article we are syncing between systems  has a title and a rich text body. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4ghppy6neju4zwmcmit.png) &nbsp;

It is also worth noting right now that this is also where we can see all the related taxonomy for content items in the system. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t042mzs3mocfaj3v7wyp.png) &nbsp;

[Setting up the trigger](https://doc.sitecore.com/xp/en/developers/connect-for-ch/50/connect-for-content-hub/create-a-trigger.html) is fairly simple. We define basic information about the trigger. Here I want to make sure I check all the objectives, since I might be mutating the object later, and the execution type as "in background" so it does not stall out any of the processing. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dewkss1mr1xl7lb6ws9z.png) &nbsp;

The conditions can become quote specific. In this example I want to sync the object when a user has fully published the news item. The content item of "M.Content" has the information about the publish state and there is a separate publish flag that was added on the news item itself.  I need to make sure both of these conditions are matched before I perform my action. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufwocoipa1te9rb4g4ak.png) &nbsp;

Now all we have to do is tie in the action we created earlier and we are all set to start sending out information.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qz49athv0l4lwyxswg1.png) 

&nbsp; &nbsp; &nbsp;
## Setup the CMP connection on XM Cloud
In your Sitecore XM Cloud deploy portal you will need to setup a few values to get your CMP functionality working.

- **SITECORE_AppSettings_cmpEnabled_define** - Enables the CMP connector in the system. (yes or no)
- **Sitecore_ConnectionStrings_CMP_dot_ContentHub** - Connection string to content hub. You can reuse your DAM OAuth connection string here.
- **Sitecore_ConnectionStrings_CMP_dot_ServiceBusSubscription** - Name of the azure service bus subscription. <u>More on this later</u>.
- **Sitecore_ConnectionStrings_CMP_dot_ServiceBusEntityPathOut** - HUB IN connection string from the service bus action that was setup.
- **Sitecore_ConnectionStrings_CMP_dot_ServiceBusEntityPathIn** - HUB OUT connection string from the service bus action that was setup

&nbsp; &nbsp;
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zu9qoakfapxzm5obszm5.png) &nbsp;


This will cause the CMP items to start showing up inside of Sitecore.

&nbsp; &nbsp;
Before:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kiesabiis7nc3t62rcfl.png) &nbsp;

&nbsp; &nbsp;
After:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p0n983bsw68w207n25r.png) &nbsp;

&nbsp; &nbsp;
Now your application should be setup to receive information from Content Hub.

&nbsp; &nbsp; &nbsp;
---
## Some problems we ran into

&nbsp; &nbsp;
### The value for Sitecore_ConnectionStrings_CMP_dot_ServiceBusSubscription

It was not immediately apparent what the value of this was meant to be. If you are not familiar with [how a topic works on Azure service bus](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#topics-and-subscriptions) it is basically a way to allow multiple "receivers" to handle the messages posted to a topic. 

- It was not the name of the action in Content Hub.
- It was not derived from the action name.

after opening a ticket with Sitecore we did find out ours was named '**<u>hub_out_subscription</u>**'. You can try this if you are having issue with your application receiving information. you might have to open a support request to get the name of yours though.

If you are setting up your own service bus, this will not be a problem. 

&nbsp; &nbsp;
### Authentication / SSO 
Working to get this up and going we also ran into an issue with authentication. Our content hub was originally setup to **ONLY** accept connections over SSO. This was causing the OAuth connection between Content Hub and XM Cloud to fail. We needed to go into Content Hub settings and set 'EnableBasicAuthentication' to true.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfq7wbm21q3bnfqedr6e.png) &nbsp;



Setting up data mappings coming in part 2