Dynamics 365 Web API and Azure Functions v2 CRUD Operations Part 2 : Using Helpers

Hello Team,

Following up on the topic of CRUD using the Web API in Dynamics 365, in the previous post we used Postman to generate our code and then copy and paste it on our Azure Functions to interact with Dynamics.

This a perfectly acceptable approach however you will probably end up with a helper class that encapsulates the request and slowly you will be building a toolkit very similar to what it might be out there already, so let’s not reinvent the wheel.

Today we are going to have a look at one of the tools that I am currently using and it’s proving to be really useful.

David’s Yack Xrm Tools CRMWebAPI

One of the principles that David said that he tried to follow was building something lightweight and I have to say that at least the .NET part is very light which is fantastic!

First you need to authenticate, already have a post on that, and once you have the access token you will need to create the API object as follows:


var api = new CRMWebAPI(resource + "/api/data/v9.0/", result.AccessToken);

Now you can think about this api object as the Service object when using the SDK assemblies, it will allow us to perform the following actions:

–          Create Records,

–          Retrieve Records

–          Update Records

–          Delete Records

–          Execute Functions

–          Execute Actions

–          Run FetchXML Queries

Sounds familiar? Let’s have a look at some examples, David already provides a lot on his Github documentation so please consider this in addition to those.

Examples


//Create an Account Record

Dictionary accountToBeCreated = new Dictionary();

accountToBeCreated.Add("name", "Example of record");

 

Guid accountId = api.Create("accounts", accountToBeCreated).Result;

 

//Create a Contact and Associate it to the Account

Dictionary contactToBeCreated = new Dictionary();

contactToBeCreated.Add("firstname", "Mario");

contactToBeCreated.Add("lastname", "Trueba");

contactToBeCreated.Add("parentcustomerid@odata.bind", "accounts(" + accountId + ")");

 

Guid contactId = api.Create("contacts", contactToBeCreated).Result;

// WordPress didn’t like the FetchXML so a picture will do 🙂

fetchXMLforFunctions


var options = new CRMGetListOptions()

{

FormattedValues = true,

FetchXml = xml

};

var accountsResult = api.GetList("accounts", options).Result;

 

//Trigger an Action

var actionParams = new Dictionary();

actionParams.Add("RecordType", 1);

actionParams.Add("RecordId", "GUID OF THE RECORD");

actionParams.Add("RecordTypeName", "account");

 

var actionTriggered = api.ExecuteAction("msdyn_GetRecordUsers", actionParams);

 

//Update Record

var updateContact = new Dictionary();

updateContact.Add("firstname", "Pepe");

var updatedContact = api.Update("contacts", contactId, updateContact, false);

//Be careful with this one, the last parameter is whether you want to do an Upsert and by default is set to YES so don't ignore it.

As you can see, there are plenty of good reasons to jump aboard the Web API boat, I know it is a change and it is different to what we are used to, but it has been there for a while and these days thanks to the help of the community we have several tools that will make the work easier.

What if we had a project where we needed to interact with both the SDK Classes and the Web API, for example if we registered an Azure Function to trigger every time we update a Contact? Let’s have a look at it, next week! 😊

Thanks,

Mario

 

Advertisements

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 )

Google photo

You are commenting using your Google 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