Code Snippet – Execute Dynamics 365 WhoAmIRequest in Azure Function
Azure Function is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure.
We can leverage ‘Azure Functions’ in Dynamics 365 to build robust integrations.
Scenario:
Lets take a scenario, where your Customer has a Facebook page and complaints posted on page should get created as ‘Case’ records in your Dynamics application.
In the above scenario,
- Connecting to Facebook and retrieving Posts can be achieved using ‘Logic Apps’ Facebook connector
- Now creating Posts as ‘Cases’ in Dynamics can be done by creating an ‘Azure Function’ with Case create logic and invoke it from ‘Logic App’
In this article, I will walk you through the steps to establish connection to D365 and execute ‘WhoAmIRequest’ from ‘Azure Functions’.
Steps to create Azure Function:
- Refer my previous article for steps to create Azure Function.
Prerequisites to Connect to D365 From Azure Function:
- We would need ‘CRM SDK’ nuget packages in Azure Function to establish connection with D365.
- Below are steps to add nuget packages to ‘Azure Function’
- Connect to ‘Advanced tools(Kudu)‘ from ‘Function Apps -> Platform features‘
- Click on ‘Debug Console -> CMD’
- From the folder explorer, navigate to ‘site -> wwwroot‘ folder
- Open the folder with your Azure Function name
- Since my function name is ‘WhoAmI’ and I got the ‘WhoAmI’ folder under ‘wwwroot‘
- To refer nuget packages, we have to create a new file by name ‘project.json’
- Add below package references
- Save
- Add URL and Credential details of ‘D365’ to ‘Application Settings’ of ‘Azure Function’
- Navigate to ‘Function Apps -> Platform features -> Application Settings’
- Add the URL, UserId, Password details.
Code Snippet:
Once you have the Prerequisites ready, below is the code snippet to execute ‘WhoAmIRequest’
using System.Net;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using System.Configuration;public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try{
log.Info(“Inside Try”);ClientCredentials userCredentials = new ClientCredentials();
var userName = ConfigurationManager.AppSettings[“Crm_UserName”];
var password = ConfigurationManager.AppSettings[“Crm_Password”];
var Crm_UniqueOrgUrl = ConfigurationManager.AppSettings[“Crm_UniqueOrgUrl”];
userCredentials.UserName.UserName = userName;
userCredentials.UserName.Password = password;log.Info(“userName – “+userName);
log.Info(“password – “+password);var service = new OrganizationServiceProxy(new Uri(Crm_UniqueOrgUrl + “/XRMServices/2011/Organization.svc”), null, userCredentials, null);
service.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());WhoAmIRequest reqWhoAmI = new WhoAmIRequest();
WhoAmIResponse resp = (WhoAmIResponse)service.Execute(reqWhoAmI);
var buID = resp.OrganizationId.ToString();
var userID = resp.UserId.ToString();log.Info(“Business Unit Id – “+buID);}
catch(Exception ex)
{
log.Info(“Exception – “+ex.Message);
}return req.CreateResponse(HttpStatusCode.OK, “Successfully made call to D365.”);
}
Run and Test the Code:
- Click on ‘Run’ and expand ‘Logs’ section to track the logs.
🙂
-
July 15, 2018 at 8:23 PMDynamics 365 – Using WebHooks to post data from Plugin to Azure Function | Rajeev Pentyala - Dynamics 365 Blog