Here is an example of a simple Azure Function that refers our CRM assemblies and creates contact record in CRM.
Log in to Azure Portal, and perform the following actions:
- Optionally, Create a Resource Group, so that it is easy to maintain the resources.
- Search for “Function App” and create one. NOTE: The App Name of the Function App should be a globally unique name.
- Add (+) a new function (GenericWebHook-Csharp template)
- This creates a new function (“CallCRMApps” in this example).
- Select the function, and under View Files (Right end of your screen), add a new file and give it a name, for example “project.json”. NOTE: It is within this file we will refer our Nuget Packages that we need in our function.
- Click on project.json and write the following code:
NOTE: At the time of writing this article the latest version of Nuget was “220.127.116.11”.
- Click Save and Run and notice the Log (bottom of the screen).
- Now click on run.csx file. Write the following sample code for the Azure Function. Please replace the highlighted Organization Serve with your D365 Organization Service Endpoint Address, also the username and password for your D365 environment.
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
log.Info(“C# HTTP trigger function processed a request.”);
// parse query parameter
string firstname = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, “firstname”, true) == 0)
string lastname = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, “lastname”, true) == 0)
IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(“<your organization service>“));
AuthenticationCredentials authCredentials = new AuthenticationCredentials();
authCredentials.ClientCredentials.UserName.UserName = “<username>“;
authCredentials.ClientCredentials.UserName.Password = “<password>“;
AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);
OrganizationServiceProxy organizationProxy = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);
Entity contact = new Entity(“contact”);
contact.Attributes[“firstname”] = firstname;
contact.Attributes[“lastname”] = lastname;
var contactId = organizationProxy.Create(contact);
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
string fullname = “”;
return fullname == null
? req.CreateResponse(HttpStatusCode.BadRequest, “Please pass a name on the query string or in the request body”)
: req.CreateResponse(HttpStatusCode.OK, “Contact created in CRM ” + contactId.ToString());
NOTE: To get your organization service , in your D365 environment click, Settings, Customizations, Developer Resources and copy the Endpoint Address under Organization Service.
Add a query under Test tab to enter the First Name and Last Name (click + Add parameter) and click Run.
If your query is OK, you will get a Status: 200 OK and the output stating that a new Contact is created in D365.
Check the D365 Contact and search for the contact.