This blog post demonstrates how to perform Create, Read, Update and Delete operations using the Dynamics CRM Web API endpoint using JQuery.

Prerequisites

This blog post assumes some knowledge of JavaScript, Visual Studio and Dynamics CRM Development using the Web API endpoint.

This blog post also assumes that an application has been configured in Azure Active Directory as per Part 2 of this blog series.

Source code for the demo ASP.NET MVC application can be found on Codeplex at https://crmdynamicsoauthwebapiexample.codeplex.com/.

Important Note

When running the sample application from localhost, a permission error will occur. This is caused by default browser security for Cross Origin Resource Sharing (CORS).

CORS security can be disabled in chrome by running the below command in the windows run command prompt.

chrome.exe –user-data-dir=”C:/Chrome dev session” –disable-web-security

disable-cors-security

Part 5: Dynamics CRM Create, Read, Update and Delete examples in a JQuery Web Application

Creating an Account

To create an account, enter an account name in the text box and then click on the Add Account button. The list under the Add Account button will be refreshed with the new account.

creating-an-account

 

When the Add Account button is clicked, the create account method below is invoked.

The first line of the create account method sets the URL to the accounts web API endpoint.

The following lines create the account json object that will be set as the body of POST request.

HTTP POST methods are used to create records when using the OData protocol. The below lines from the Create Accounts method creates a POST http request that is sent to the Dynamics CRM Web API endpoint.

The globalToken variable that contains the authorization token (see part 4 of this blog series to find out hot the globalToken variable is set) is added to the JQuery ajax object

and is used to set a request header in the createAccountsBeforeSendCallback . If the Auzthorization header is not set, then a 401 unauthorized error will occur. This applies to all requests.

globaltoken-variable

 

createAccountsBeforeSendCallback  also add a JSON content type header. If the JSON content type header is not set, then a 415 Unsupported Media Type error will occur. The JSON content type header is required for any request body that contains JSON. Requests with a JSON  body is required when creating and updating accounts.

requests-with-json-body

Once the create account request has completed successfully, the list of accounts are refreshed.

Reading Accounts

Accounts are displayed in a bulleted list in the demo web application. The GetAccounts method is responsible for retrieving all accounts from the Dynamics CRM Web API endpoint.

The first line of the create account method sets the URL to the accounts web API endpoint.

HTTP GET methods are used to read/query records when using the OData protocol. The below lines from the Get Accounts method creates a GET http request that is sent to the Dynamics CRM Web API endpoint.

 

 

 

As with the createAccountsBeforeSendCallback  method, the getAccountsBeforeSendCallback  method sets the authorization request header.

Updating an Account

When an Update link is clicked the update account method, below, is invoked.

The first two lines of the update account method sets the URL to the accounts web API endpoint. This URL points to a single account with a matching account id of the clicked-on account.

The following lines create the account json object that will be set as the body of the PATCH request.

HTTP PATCH methods are used to update records when using the OData protocol. Patching replaces Dynamics CRM record fields with the values specified in the JSON body.

A HTTP PUT method is an alternative for updating records however unlike PATCH, PUT sets Dynamics CRM field to null if the field is not included in the request body.

The below lines from the update account method creates a PATCH http request that is sent to the Dynamics CRM Web API endpoint.

As with the createAccountsBeforeSendCallback  method, the updateAccountsBeforeSendCallback  method sets the authorization request header and the JSON content type header.

Once the update account request has completed successfully, the list of accounts are refreshed.

 

Deleting Accounts

When a delete link is clicked the deleteAccount method, below, is invoked:

The first two lines of the delete account method sets the URL to the accounts web API endpoint. This URL points to a single account with a matching account id of the clicked-on account.

HTTP DELETE methods are used to delete records when using the OData protocol. The below lines from the delete account method creates a PATCH http request that is sent to the Dynamics CRM Web API endpoint.

As with the createAccountsBeforeSendCallback  method the

deleteAccountsBeforeSendCallback method sets the authorization request header.

Once the delete account request has completed successfully, the list of accounts are refreshed.

 

In summary, this blog shows how to use the Dynamics CRM Web API endpoint from an external website using JQuery to create OData requests. The full source code for the sample can be found here https://crmdynamicsoauthwebapiexample.codeplex.com.