Introduction :
Microsoft CRM 2016 introduced a new Web
API concept. This will make it easier to create applications across a wide
variety of platforms, devices, and programming languages.
You can perform all operations using HTTP
requests with the Web API located at
[organization uri]/api/data/v8.0/. The Web API implements OData version 4.0, an open
standard for data access.
We can perform following operations using
web API
· Create
·
Update
·
Delete
·
Retrieve
·
Retrieve multiple( using odata query and fetch)
·
Execute Web API functions
·
Execute Web API Actions
·
Execute Web API Query Functions
Let’s check where the web API is located.
I am using online ms crm 2016
It showed
me some JSON object like below
{
"@odata.context":"https://bareillly.crm.dynamics.com/api/data/v8.0/$metadata","value":[
{
"name":"accountleadscollection","kind":"EntitySet","url":"accountleadscollection"
},{
"name":"accounts","kind":"EntitySet","url":"accounts"
}
Explanation
:
name
: This is the name
of the entity set.
kind :
For the web API only Entity
sets are listed.
url
: It represents the part of the resource path to retrieve data for
the entity.
How to use Web API:
We can use XMLHttpRequest object to perform all the above operation
with Web API.
Example: Create an
Account using Web API:
function CreateRecord(clientURL,entityType,entityData)
{
var req = new XMLHttpRequest()
req.open("POST",encodeURI(clientURL + "/api/data/v8.0/" +entityType), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 204) {
var accountUri = this.getResponseHeader("OData-EntityId");
console.log("Created account with URI: "+ accountUri)
}
else {
var error = JSON.parse(this.response).error;
console.log(error.message);
}
}
};
req.send(JSON.stringify(entityData));
}
// This function Creates an Account
function CreateAccountUsingWebAPI() {
debugger;
var entityType ="accounts"
var clientURL = Xrm.Page.context.getClientUrl();
var Account ={};
Account["name"] ="My first Account by API";
Account["accountnumber"] = "9211";
Account["fax"] ="0581-231383";
CreateRecord(clientURL,entityType,Account)
}
No comments:
Post a Comment