In today’s past I will show you how we can call Azure function from Dynamics CRM plugin with the help of WebClient and HttpClient classes.
Lets say we have a requirement to send Lead data to Azure function for further processing. For this post data will be send as request querystring to azure function from CRM plugin. We can also use same code to send data in request body.
Plugin is registered on Create message, Post event pipeling and mode is asynchronous.
Code to call azure function using WebClient
public void Execute(IServiceProvider serviceProvider) { //Extract the tracing service for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); string url = "https://function.azurewebsites.net/api/function1?code={FunctionCode}";; try { if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { //Lead Entity leadTarget = context.InputParameters["Target"] as Entity; string queryStringParam = string.Empty; if (leadTarget.Attributes.Contains("firstname") && leadTarget["firstname"] != null) { queryStringParam += "&FirstName=" + leadTarget.GetAttributeValue("firstname"); } if (leadTarget.Attributes.Contains("lastname") && leadTarget["lastname"] != null) { queryStringParam += "&LastName=" + leadTarget.GetAttributeValue("lastname"); } if (leadTarget.Attributes.Contains("emailaddress1") && leadTarget["emailaddress1"] != null) { queryStringParam += "&Email=" + leadTarget.GetAttributeValue("emailaddress1"); } if (leadTarget.Attributes.Contains("mobilephone") && leadTarget["mobilephone"] != null) { queryStringParam += "&Phone=" + leadTarget.GetAttributeValue("mobilephone"); } Entity lead = new Entity("lead"); lead.Id = leadTarget.Id; using (WebClient client = new WebClient()) { byte[] response = client.DownloadData(url+ queryStringParam); string responsestr = Encoding.UTF8.GetString(response); lead["description"] = responsestr; service.Update(lead); } } } catch (Exception ex) { throw new InvalidPluginExecutionException("Error:" + ex.Message + " Inner Exception " + ex.InnerException); } }
Key Points –
- Replace Azure function URL and code part based on your azure function hosted on azure portal
- You could also configure this in CRM custom entity as key/value pair. Also you can use Environment variable feature which is introduced recently in CRM online
- We are storing the response from azure function in Lead entity description field
- You could also create custom field to store the status of Azure function call
- Finally If any error thrown from Azure function will be logged in system jobs
Code to call azure function using HttpClient
public void Execute(IServiceProvider serviceProvider) { //Extract the tracing service for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); string url = "https://function.azurewebsites.net/api/function1?code={FunctionCode}"; try { if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { //Lead Entity leadTarget = context.InputParameters["Target"] as Entity; string queryStringParam = string.Empty; if (leadTarget.Attributes.Contains("firstname") && leadTarget["firstname"] != null) { queryStringParam += "&FirstName=" + leadTarget.GetAttributeValue("firstname"); } if (leadTarget.Attributes.Contains("lastname") && leadTarget["lastname"] != null) { queryStringParam += "&LastName=" + leadTarget.GetAttributeValue("lastname"); } if (leadTarget.Attributes.Contains("emailaddress1") && leadTarget["emailaddress1"] != null) { queryStringParam += "&Email=" + leadTarget.GetAttributeValue("emailaddress1"); } if (leadTarget.Attributes.Contains("mobilephone") && leadTarget["mobilephone"] != null) { queryStringParam += "&Phone=" + leadTarget.GetAttributeValue("mobilephone"); } Entity lead = new Entity("lead"); lead.Id = leadTarget.Id; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = client.GetAsync(url+ queryStringParam).Result; lead["new_status"] = response.StatusCode.ToString() + response.IsSuccessStatusCode + response.ReasonPhrase; ; service.Update(lead); } } } catch (Exception ex) { throw new InvalidPluginExecutionException("Error:" + ex.Message + " Inner Exception " + ex.InnerException); } }
Hope this helps!
2 thoughts on “Dynamics 365 different ways of calling Azure Function from CRM plugin”