Retrieving Records from Dynamics 365 using Azure Function in Visual Studio

In this article, you will learn a step by step approach for retrieving records from Dynamics 365 using Azure Function in Visual Studio.

Introduction

Microsoft Azure is a cloud-based service for developing, testing, and managing applications. It supports different programming languages and frameworks including both Microsoft and third-party solutions.
Microsoft Azure function is an easy solution for running small pieces of code independently without worrying about the whole infrastructure of the application.

Interested in learning more about the Azure Functions? Click below to read the Microsoft Azure Functions official documentation: https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview

Prerequisites:

The prerequisites to retrieve the records from Dynamics 365 include:

  • Visual Studio 2019 IDE to write and execute the code
  • PFE Libraries which help us in retrieving the records.

There are two main advantages of using PFE Libraries:

  • Provide effective and authentic connection with CRM
  • Handle parallel requests to CRM

Download and Install Visual Studio 2019.

You can download PFE libraries directly through this link: https://codeplexarchive.blob.core.windows.net/archive/projects/pfexrmcore/pfexrmcore.zip

Find out more about our Dynamics 365 services.

1-   Azure Project

Open Visual Studio and create an Azure Function Project.

Open Visual Studio

Azure Function Project

Select the highlighted settings for your project to get started.

 

2-   Add PFE Libraries:

After creating Azure Function project, add the PFE libraries to the solution. Paste these two files in the solution folder from:

pfexrmcore > sourceCode > sourceCode > Samples > PfeXrmCoreWalkThroughApp

  • SampleConfig.cs
  • ServiceManagerContext.cs

Add this function in Function1 class.

 

private static OrganizationServiceManager GetOrganizationServiceProxy()
{
     try
     {
         var orgName = “TESTORG”;                            // CRM Organization Name
         var userName = “[email protected]”;   // User Name
         var password = “testuser”;                          // Password
         var uri = XrmServiceUriFactory.CreateOnlineOrganizationServiceUri(orgName);
         var serviceManager = new OrganizationServiceManager(uri, userName, password);
         return serviceManager;
     }
     catch (Exception ex)
     {
          throw ex;
     }
}

 

This function will establish a connection with Dynamics 365 Online. For the time being, the CRM credentials are hardcoded in the portal, but this information will be updated post-deployment. For now, we are only using the CRM credentials for testing purposes and have the flexibility to change it later.

Dynamics 365 Online hardcoded

You will need to add this  code in your Task function, refer to the image below:

 

var orgProxy = GetOrganizationServiceProxy();

Task funcation

3-   NuGet Packages:

Right-click on the project and select Manage NuGet Packages to install these:

  • NET.Sdk.Functions
  • Pfe.Xrm.CoreV8
  • Configuration.ConfigurationManager

Add these namespaces:

  • using Microsoft.Pfe.Xrm
  • using System.Configuration
  • using System

4-   Get FetchXML from D365

For records retrieval, you must construct a FetchXML or you can download it from Dynamics 365.

Get FetchXML from D365

 

download fetchxml dynamics 365

 

After downloading the FetchXML, Paste it to your code.

downloading the FetchXML

Now we must write a query to retrieve the records from CRM using FetchXML.

Paste this code after the FetchXML.

 

var Query = new Dictionary<string, QueryBase>();
Query.Add("accounts", new FetchExpression(FetchXML));
var Results = orgProxy.ParallelProxy.RetrieveMultiple(Query, true)["accounts"].Entities;

 

After all the accounts are retrieved from the Dynamics 365, we need to display them and test if the records have been retrieved or not. For this, we will need to add the following lines of code in the Task Function:

 

foreach (Entity Result in Results)
{
   // Displaying all Accounts name...
   log.Info("Successfully Retrieved---------> " + Result.Attributes["name"].ToString());
}

 

To test the statement above, you need to add the following response to the log file to ensure whether the function is executed successfully or not.

return req.CreateResponse(HttpStatusCode.OK, "Successful");

Your complete Task Function code will look like this

Task Function code

If you face any errors, please check for missing namespaces.

missing namespaces

Now you will need to build your program. To do that open the Console Window, copy the URL and RUN this in the browser.

After you run the URL in the browser, your results will be displayed in your Console from where you can retrieve them.

Console Window

The purpose of this blogpost was to give you all a step by step tutorial for retrieving records from Dynamics 365 using the Azure Function in Visual Studio. I hope that this blogpost has helped give you some clarity on the process. If you have any insights and questions for us, please leave in a comment below.

Find out more about our Dynamics 365 services
In our next post, we will show you how you can deploy this function to the Azure Subscription, and dynamically connect with the Dynamics 365 Org. If you have any questions or queries, leave a comment below. You can also connect with our BOLDEnthusiasts by clicking here.

Stay tuned!

2 thoughts on “Retrieving Records from Dynamics 365 using Azure Function in Visual Studio”

  1. Very Informative. Just a little confusion, you are saying to paste two files but the files are not listed?

Leave a Reply

Your email address will not be published. Required fields are marked *