Home > Azure, CRM > Code Snippet – Execute Dynamics 365 WhoAmIRequest in Azure Function

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
    • WhoAmi_1
    • Click on ‘Debug Console -> CMD’
    • WhoAmi_2
    • 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
    • WhoAmi_3
    • To refer nuget packages, we have to create a new file by name ‘project.json’
    • WhoAmi_4
    • Add below package references
    • WhoAmi_5
    • Save
  • Add URL and Credential details of ‘D365’ to ‘Application Settings’ of ‘Azure Function’
    • Navigate to ‘Function Apps -> Platform features -> Application Settings’
    • WhoAmi_6
    • Add the URL, UserId, Password details.
    • AzFunc_5

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.

WhoAmi_7

🙂

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: