Note: Please be advised, this post most likely contains grammar mistakes I am sorry for that but I was in a hurry and wanted to share this with you all. Anyway; enjoy…
Once upon a time…
there was this BizTalk Integrator who was wondering how hard it would be to get LinkedIn profile information and store this in Microsoft Dynamics CRM 2011. He didn’t waste a lot of time and within no time he produced a diagram showing on a high level how he intended to tackle this issue.
-
Redirect user to LINKEDIN to get Authorization Code.
-
Authorization code is send back.
-
Request OAUTH 2.0 Access Token using obtained authorization code
-
OAUTH Access token is send back.
-
Establish Service Bus Connection. Create a Topic, Subscription and Filters if they don’t exists. Create message containing OAUTH Access token and set brokered message properties. Once done publish message.
-
BizTalk retrieves message from Topic Subscription.
-
BizTalk creates a GET Request including the OAUTH Token and sends it to the LinkedIn api to request the user’s information.
-
BizTalk receives a response from LinkedIn, and creates a new Atom Feed message containing the Contact Properties required to add a new contact in CRM
-
BizTalk sends a POST request to the CRM 2011 XRMServices/2011/OrganizationData.svc/ContactSet/ endpoint such that a new Contact is created
Okay all kidding aside; in this blog post I will dive deeper in to the above mentioned scenario, and will highlight the ins-and-outs as mentioned in steps 1 to 9. So let’s get started.
For all those, not interested in reading about it; but rather want to see the pre-recorded demo. Click here
Website (steps 1 to 5) – Get OAUTH token and send it to Service bus
One of the first challenges we have to tackle consist of obtaining a token from LinkedIn which allows us to perform authorized requests against a certain user profile. In English; in order to get LinkedIn profile information we need to acquire approval from the profile owner. Once we have obtained this permission we need to obtain an OATH 2.0 token which can then be used to authorize our requests to the LinkedIn API.
So how did I solve this ‘challenge’, well I build this small website based on MVC 4* and hosted it for free in Azure. This website uses the default MVC 4 template, and all I did is change some text and added a new controller and subsequent view. The controller, which I gave a self-explanatory name ‘LinkedInController’ takes care of all logic required (getting the OAUTH token and sending it so Windows Azure Service Bus).
Read all about MVC here, and in order to learn more about MVC and other cool stuff go to Pluralsight)
Below a more detailed overview, on how I took care of steps 1 to 4.
Note: I gathered the information on how to authenticate using OAUTH 2.0 from this page
1) Obtaining profile user permission.
On the default action (Index) in my case, I perform a good-old fashioned redirect to a LinkedIn site in order to obtain the user’s authorization (permission) to query his/hers LinkedIn profile. On this site the user is presented with the following screen.
2) Obtaining linkedIN’s authorization code.
Once the user has allowed access; he or she will be redirected back to the mvc website. This redirection request from LinkedIn contains a Authorization_Code parameter and points to a specific Action within the LinkedIn Controller.This action will extract the Authorization Code and this code will be stored internally.
3) Request OATH 2.0 token.
Next a HTTP Post to the LinkedIn API will be executed, containing the users authorization code and some other vital information
4) Process JSON response.
As a result we retrieve a JSON response which is serialized back into a custom object.
5) Publish message to windows azure service bus.
Once we’ve filled out custom object, I send it to a custom build class*, which then sends this object to a Windows Azure Topic.
* The custom class I’ve build is my own implementation of the REST API for creating TOPICs, Subscriptions, Filters and sending messages to Windows Azure Service Bus (see Service Bus REST API Reference). I’ve build this class a while back and the main reason I;’ve build it, is such that I can reuse it in my Demo Windows Phone Apps and Demo Windows Store Apps). I will not list the code here, however if you are interested just send me an email and I’ll send it to you
The above mentioned class requires that some specific information is passed in, this is explained below
Payload
The first required parameter is of type Object and should contain the message object which is to be sent to service bus.
Brokered message Properties
This parameter contains the key and value pairs adding additional context to the message send to service bus. In my demo I’ve added the following key value pairs to the message. The importance of these properties will be made clear in a little while.
Source=linkedIN Method=select Object=personalData Auth=[DYNAMIC]
Note: Auth will be replaced in code with the actual OAuth 2.0 token received earlier and is added to the context of the message. Later on this is used by BizTalk (but more on this later)
Service bus name space
One of the required parameters is the service namespace which is to be used. In my demo I created a namespace called brauwers
A service namespace provides a scoping container for addressing Service Bus resources within your application.
Service bus Management Credentials
Two other parameters which are required, consist of the so-called issuer-name and issuer-token. These credentials are required if we want to perform actions on the service bus resource like (posting messages, or performing management operations). My component requries that you supply the owner (or full access) credentials as this component will try to perform some management operations (creating subscriptions) which needs these elevated privileges
Service bus Topic name
This parameter should consist of the name of the Topic to which the message will be published. In my demo I’ve chosen the value “SocialAccessTokens”.
Note: in case the topic does not exists the custom class will create it (hence the reason we need to use an issuer who has elevated (create) privileges
Service bus Topic subscription
This parameter holds the desired topic subscription name, which we want to create. In my demo I’ve chosen the value “LinkedINSubscription”
Note: Eventually a filter will be added to this subscription, such that messages matching a this filter or rule (see brokered message properties mentioned earlier) will be ‘attached’ to this subscription. The importance of this, will be made clear further down in this blog post when we will explain how BizTalk is configured). On another note; in case this subscription does not exist a the custom class will create it (hence the reason we need to use an issuer who has elevated (create) privileges.
Service bus Topic Filter name
This parameter contains the name to use for the filter, which will be attached to the topic subscription.
Service bus Topic Filter expression (Sqlfilter)
This parameter is actually pretty important as this parameter defines the rule which applies to the subscription it is attached to. The filter I used in my example –> Source = ‘linkedIN’ (keep in mind that we set a brokered message property earlier containing these values)
Note: the filter in my example should be interpreted as follows: messages send to the topic (SocialAccessTokens) which contain a brokered message property key named source, and of which the value matches the string “linkedIN” should be ‘attached’ to the subscription “LinkedINSubscription”.
BizTalk (steps 6 to 9) – Connecting the dots, from service bus to crm2011
Well the previous steps; ensured that a message containing the OAUTH 2.0 token, required to perform LinkedIn profile requests on behalf of the profile owner. The next steps should result in a new contact in Dynamics CRM 2011 based upon the data retrieved from LinkedIn. These steps involve BizTalk and some of it’s ‘new’ (or let’s reformulate this: out of the box) capabilities, consisting of the SB-Message adapter and the WCF-WebHttp Adapter.
So let’s get started.
6) Setting up BizTalk such that is connects to windows azure service bus.
At this point a message has been delivered to a Windows Azure Service Bus topic. However now we need to pull this message into BizTalk and for this to work we need to perform a few steps like
- a) defining a property schema used to capture the custom brokered message properties.
- b) define a schema which reflects the message send to windows azure service bus
- c) deploy the artifacts
- d) configure a receive port and location to get the service bus message(s) in BizTalk
These steps will be explained further below
Defining a property schema used to capture the custom brokered message properties.
Note: Before proceeding ensure you’ve created a new solution and BizTalk project and don’t forget to sign it with a strong key and give it an application name.
In step 5; we added a few custom properties to the message context consisting of the keys “Source,Method,Object and Auth” . In my example I want to use these properties as Promoted Properties within BizTalk, such that I can do some nice stuff with them (more about this in step 7). So in order to cater for this we need to create a new Property Schema in BizTalk and all this schema needs to contain are elements which are named exactly like the brokered message property key names.
Define a schema which reflects the message send to windows azure service bus
In order for BizTalk to recognize a message it’s message definition needs to be known internally. In English; if we want to work with the message as send to windows azure service bus in BizTalk, we need to ensure that BizTalk can recognize this message and thus we need to define. How this is done is explained below
- In my example I send a message to the service bus; this message is actually a representation of an internal defined class. This class is defined as mentioned below:
- So how do we translate this message to a BizTalk Schema? Well we have a few options, and I’ll use the not so obvious approach and that’s the manual approach. In order to do so, we need to create a new schema in BizTalk which has the following properties and structure:Target Namespace should match the DataContract Namespace, thus in my case : http://brauwers.nl/socialauthenticator/linkedin/v1.0/ The Main Root should match the class name, thus in my case :ApiAccessInformation The main root should contain the following elements, all of them of type string 1) apiName 2) apiClientId 3) apiClientSecret 4) apiAccessScope 5) authorizationKeyThe end result should look like depicted in the image below
Deploy the artifacts
So far so good, but first let’s deploy the BizTalk project, and once it is deployed we can go ahead and start configuring it. (I named my BizTalk Application CRM2011)
Set up BizTalk Receive Port and Receive Location
So far so good, but now we need to put everything together such that we can configure BizTalk to read the Service Bus Topic Subscription and process any messages published to it. How this is done is explained below.
- Go to your just deployed BizTalk Application and create a one-way receive-port and name it appropriately.
- Created a new receive location, choose the SB-Messaging adapter, select your receive host and select the xml_receive pipeline component.
- Configured the SB-Messaging adapter. Enter the appropriate subscription url. In my case this would be: sb://brauwers.servicebus.windows.net/socialaccesstokens/subscriptions/LinkedINSubscription sb://[NAMESPACE].servicebus.windows.net/[TOPIC_NAME]/subscriptions/[SUBSCRIPTION CONTAINING MESSAGES]
- Fill out the required authentication information, consisting of the Access Control Service uri and a valid Issuer Name and Issuer Key
- Select the property schema to use such that the Brokered Message Properties defined earlier are treated as Promoted Properties within BizTalk.
- Press OK
7) Setting up BizTalk such that the LinkedIn API can be queried.
At this point we’ve set up BizTalk such that it can retrieve the messages published to windows azure service bus and tread the brokered message properties as Promoted properties. This section will now list and explain the next steps which are needed in order to query the LinkedIn API using the wcf-webhttp adapter.
Creating a new send port leveraging the new wcf-webhttp adapter
So how do we configure the wcf-webhttp adapter such that we can send a GET request to the linkedIn API, well the steps below will show how this is achieved.
- Create a new static solicit-response port and name it appropriately, choose the WCF-WebHttp adapter, choose your prefered send handler and select XML_Transmit as Send Pipeline and XML_Receive as Receive Pipeline
- Now click on configure, such that we can configure the WCF-WebHttp adapter.
- First configure the Endpoint address; I’ve used https://api.linkedin.com/v1/people
- Now we set the HTTP Method and URL Mapping, for this I use the BtsHttpUrlMapping element, which contains an operation method which is set to GET and the rest of the Address Uri which consists of a list of profile fields we want to select and an oauth2 access token which value we will set using variable mapping (step 5 below)
- In our BtsHttpUrlMapping we have set a dynamic parameter which was named {Auth}. The value we want to assign to this, will be the oAuth 2.0 token which is available to us as a promoted property (see explanation step 6 “Setting up BizTalk such that is connects to windows azure service bus“). So for Propery Name we select the element name containing the promoted propery value (in my case Auth) and for property namespace we enter the namespace name of our created property schema.
- Once done press OK.
- Now select the security tab and ensure that the security mode is set to Transport and the Transport client credential type is set to NONE
- Now go to the Messages Tab and ensure that the following Outbound Http Headers are set Accept: application/atom+xml; Content-Type: application/atom+xml;charset=utf-8 Accept-Encoding: gzip, deflate User-Agent: BizTalk
- Make sure that the outbound message is surpressed, by indicating the GET verb-name
8) Dealing with the response message received from LinkedIn.
Well at this point we have send a GET request to linkedIn, however we haven’t set up the logic yet to cater for processing the response message send back to BizTalk by the LinkedIn API. So let’s dive in to it straight away.
Figuring out the response format from LinkedIn
In order to figure out the response message which is sent back to BizTalk, I have used Fiddler and composed a GET request as it would be sent by BizTalk.
- Open fiddler, and go to the compose tab, and use the following Uri, and replace [TOKEN] with the OAUTH token (note: you can get your token by going to the web application, but be aware if you allow access to your profile, you will be part of my DEMO and as a result your profile information will be stored in my Demo CRM environment ) https://api.linkedin.com/v1/people~:(first-name,last-name,email-address,date-of-birth,main-address,primary-twitter-account,picture-url,headline,interests,summary,languages,skills,specialties,educations,certifications,courses,three-current-positions,three-past-positions,following,job-bookmarks,phone-numbers,bound-account-types,twitter-accounts,im-accounts)?oauth2_access_token=[TOKEN]
- Add the following request headers Accept: application/atom+xml; Content-Type: application/atom+xml;charset=utf-8 Accept-Encoding: gzip, deflate User-Agent: BizTalk
- Press Execute, and double click on the 200 Response, and select the RAW tab
- Copy and past the response message, and store it as an xml file
- Now go back to your BizTalk Solution and right click in your project and select –> Add new generated item)
- Select Generate Schema
- Select Well-Formed XML and browse to your saved XML file containing the response and press OK
- A new schema should be created and it should look similar to the one displayed below
9) Transforming the LinkedIn Profile RESPONSE and constructing a CRM POST request.
Well at this point we have the response definition, and now we are ready for constructing our POST request which eventually will ensure that we store the data in CRM. So let’s get started with these final pieces.
Figuring out the request message structure needed for inserting data into CRM
In order to figure out the request or payload message which is sent as body in the POST request to CRM, I have used this CRM Codeplex project CRM ODATA Query Designer Below the steps I performed to obtain the correct XML structure (note you need to have installed the codeplex project)
- Open up CRM in your browser and select the CRM Odata Query Designer
- Select ContactSet
- Press Execute and switch to the results (ATOM) tab
- Now look for the first ID and copy the url
- Copy this url in your browser and then select source
- Copy the result xml, and store it locally
- Open the xml file and let’s clean it up, such that only those fields are left which matter to you. See below my cleaned-up xml structure (please note the elements named new_ ,these are custom fields I’ve added)
- Now open up your BizTalk Solution, right click and select the “Add new generated item” like you did before and generate a schema using the well-formed xml.
- An xsd schema has been created, and it should look similar to the one depicted below. Note that I manually added the min and max occurance
Create a BizTalk Mapping which creates the CRM Insert Request
At this point we can create a mapping between the LinkedInResponse message and the CRM Insert message; I’ve done so and named this map “LinkedInPersonResponse_TO_AtomEntryRequest_CrmInsert.btm”
Build the BizTalk solution and Deploy
Now that we have all artifacts ready, we can deploy our BizTalk Solution once again and finish up the last part of our BizTalk configuration.
Creating a new send port leveraging the new WCF-WebHttp adapter to send a POST request to CRM
Our final step, before testing, consists of adding one last send port which leverages the WCF-WebHttp adapter once more; but this time we will be performing a HTTP POST to the crm endpoint (..XRMServices/2011/OrganizationData.svc/ContactSet/) which will result in a new contact record in CRM 2011. Below I’ll show you how to configure this last sendport.
- Create a new static One-Wayport and name it appropriately, choose the WCF-WebHttp adapter, choose your prefered send handler and select XML_Transmit as Send Pipeline
- Now click on configure, such that we can configure the WCF-WebHttp adapter.
- First configure the Endpoint address, which should point to an address ending on OrganisationData.svc/ContactSet .The full endpoint I’ve used is http://lb-2008-s07/Motion10/XRMServices/2011/OrganizationData.svc/ContactSet/
- Now we set the HTTP Method and URL Mapping, this is pretty straightforward as we only have to add the verb POST.
- Now select the security tab and ensure that the security mode is set to TransportCredentialOnly and the Transport client credential type is set to NTLM
- Now go to the Messages Tab and ensure that the following Outbound Http Headers are set User-Agent: BizTalk Content-Type: application/atom+xml
- Once done press OK
- Now select the outbound maps section, and select the map you’ve created and deployed earlier.
- Last but not least, add a Filter, stating “BTS.MessageType = person” such that the we can route the LinkedIn Response message (Person) to this sendport.
- At this point, we are done and we can now start the application and see if everything works.
Note: in order to be able to add records to CRM, you need to ensure that you’ve added the Host Intance User Account used for the selected Send Handler in CRM and granted this account the correct permissions. Failure doing so, will result in a 400 Bad Request which initially will throw you off. Below I’ve added a few screenshots The account svc-bts trustetd is the account used by the LowLatency_Host which I use to connect to the XRM service from CRM. Subsequently I’ve created an account in CRM2011 for this user and added this account (for testing purposes) to the System Administrator role.
Click Here to see a video showcasing above mentioned scenario