Send Data from CRM Online to Azure bus - part 1

Beginning : Where we are

In few words, today we create an opportunity in the CRM, the data are retrieve with some SSIS package and consolidated on our SQL server, then Sharepoint is "eating" the data from SQL to create a Sharepoint site with the correct metadata, and to finish the CRM is retrieving the link of the Sharepoint Site. In a quick picture it's like :

Today scenario

Where we want to go

In our working environment, we have three main systems : Sharepoint, Dynamics CRM and some SQL servers.
The project we are working on will help to improve the exchange of Data between the three systems to complete a particular process. Quick picture to give you an idea :
Goal

Now we will focus on the relation between Dynamics CRM and Azure Bus

As described above, we want to send messages to several systems once we update the CRM or Sharepoint, that's why we are going to use a Topic which is able to handle several subscribers.

First step : we need to create our Topic in Azure Service Bus

To do so, you have to create your first Topic by browsing the azure portal (at the post time, still in old portal)
service bus azure

then go to the Topic tab :
topic tab

from here you can create your first Topic subscription, give it a name and for our example, we will leave the other parameters as it is. In our case, we create a topic which will be mainly used to handle Opportunities events.

You now have your first topic and we are ready to send data.

Second step : Link any CRM event to Azure Bus

This step is pretty easy, you will need to dig and find your favorite tool to register the Plugins : "Plugin Registration Tool" if you don't have it (CRM SDK 2015 : https://www.microsoft.com/en-us/download/details.aspx?id=44567) , then connect to your CRM Online instance you want to play with.
In order to create a relationship between you CRM instance and you Azure Service Bus registration, you simply need to go to :
You will end up in a new window such as :
Pay attention to the four fields with the blue line :

  • Solution Namespace is related to your Azure subscription
  • Path is the name you gave to your Topic freshly created
  • Contract depends on to what you want to send the data in the Azure service Bus, in our case, we stick to the Topic.
  • Claim (using CRM Online) needs to be "None" Once those fields are fields, next step is to press "Save & Configure ACS"

You will get one more popup like this one :

  • The management key needs to be retrieved from the Azure portal on the Service bus page : and the Management Key is named "Default key" here : EDIT: Here is how to retrieve the key with the new Azure portal UI :

  • Then the Certificate file and the Issuer Name can be found in the Developper resource page of your CRM Organization.

You now have to press the "Configure ACS" button, you will get a popup that you need to approve and tada.
Close, "Save & Verify Authentication" (new popup, we love popup with CRM :)), Close, Save. And we linked the CRM with Azure Service Bus.
You now can see your new Servie Endpoint in the List :

The next step is obviously to do something with this endpoint during any triggering event on the Opportunity (or whatever entity you want). We now need to create a new step :
We will stick to the Post-Operation event (async) on Update for any opportunities for now :

As soon as you reach this step, we are good to go to test everything !
It's now getting interesting :-)

We go to our CRM instance and will update an opportunity to check if everything works fine. You now wonder to know how you are suppose to know if something worked or not, to check this, you need to find the "System Jobs" which is under Setting > System
And tada :
It's a success on CRM side obviously, now we need to check what we have in the Topic on Azure Service Bus side.
I found a great tool which allow you to check every details of your Service Bus "instance", it's called "Service Bus Explorer", you can find it here : Service Bus Explorer

To connect the tool with your Service Bus instance, you will need to Connection String which is foundable on the same page you were for the Management Key (Default key) of your Service Bus.
You will then have a nice Tree view with all you have in the Service Bus instance :
As you can see, in my example, i have four subscriptions associated to my "Opportunity" Topic. Here we will focus on the subscription "opportunitysubscription" where the content from the CRM is sent.
To see the messages on this Subscription, you will have to do as follow :
We select the Receive Mode : "Peek" to simply not delete the message once we receive it.
And here the result :
You are probably wondering why the body of the Message in looking weird, it's because by default the Service Bus is converting the data from the CRM into a Stream.
If you are curious, and you create a quick program to "decrypt" this stream, here is a sample of code to receive a read the content :

string connectionString = "Endpoint=sb://crm-testing.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=Your Default Key";

            SubscriptionClient Client =
                SubscriptionClient.CreateFromConnectionString
            (connectionString, "Your Topic Name", "Your Subscription name");

            // Configure the callback options.
            OnMessageOptions options = new OnMessageOptions();
            options.AutoComplete = false;
            options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

            Client.OnMessage((message) =>
            {
                try
                {
                    Stream stream = message.GetBody<Stream>();
                    StreamReader reader = new StreamReader(stream);
                    string s = reader.ReadToEnd();
                    System.IO.File.WriteAllText(message.MessageId, s);
                    message.Complete();

                }
                catch (Exception)
                {
                    // Indicates a problem, unlock message in subscription.
                    message.Abandon();
                }
            });`

The result will look like :

@RemoteExecutionContext/http://schemas.microsoft.com/xrm/2011/Contracts    i)http://www.w3.org/2001/XMLSchema-instance@BusinessUnitId�$58bcb6af-7879-e511-80e7-3863bb347a80@
CorrelationId�$2352076a-49b8-4f24-aa79-b09ff2647049@Depth�@InitiatingUserId�$40f20074-ede3-48cc-aafc-750a1275b99b@InputParameters    aBhttp://schemas.datacontract.org/2004/07/System.Collections.Generic@KeyValuePairOfstringanyType^key�Target^value.type�Entity@  
Attributes@KeyValuePairOfstringanyType^key�  
be_goapproved^value.type�    b:boolean   b http://www.w3.org/2001/XMLSchema�@KeyValuePairOfstringanyType^key�be_authorizedengl^value.type�    b:boolean   b http://www.w3.org/2001/XMLSchema�@KeyValuePairOfstringanyType^key�modifiedonbehalfby^value.nil�@KeyValuePairOfstringanyType^key�be_leadidentified^value.type�   b:boolean   b http://www.w3.org/2001/XMLSchema�@KeyValuePairOfstringanyType^key�estimatedclosedate^value.type�  
b:dateTime    b http://www.w3.org/2001/XMLSchema�  ًc�@KeyValuePairOfstringanyType^key�  
modifiedon^value.type�  
b:dateTime    b http://www.w3.org/2001/XMLSchema��CB �T�H@KeyValuePairOfstringanyType^key�be_grossrevenueweighted^value.type�Money@Value�       �      @KeyValuePairOfstringanyType^key�timezoneruleversionnumber^value.type�b:int b http://www.w3.org/2001/XMLSchema�@KeyValuePairOfstringanyType^key�be_grossrevenue^value.type�Money@Value�        �      @KeyValuePairOfstringanyType^key�name^value.type�b:string   b http://www.w3.org/2001/XMLSchema�CRM to Azure Service Bus  

Interesting huh ?

In the next part, we will check what are the possible scenarios to decrypt it so every systems which are subscribers at the same Topic can consume the Messages without any extra processing.

Happy CRM and Service Bus.

Useful links :

Clement

Dynamics 365 CRM & Power Platform addict.