Friday 15 January 2016

MS CRM 2016 Web API - Retrieve

This example returns data for an account entity with the primary key value equal to 9C33D98E-2FAD-E511-80DE-6C3BE5A8380C. This query also expands data from the related opportunities of Account and related tasks using the respective single-valued and collection-valued navigation properties: opportunity_parent_account and Account_Tasks. To identify the appropriate properties and navigation property names to use for the account entity, see the account EntityType

function RetrieveAccountData()
{
var organizationUrl = Xrm.Page.context.getClientUrl();

var query = "accounts(9C33D98E-2FAD-E511-80DE-6C3BE5A8380C)?$select=accountcategorycode,accountnumber,creditonhold,createdon,numberofemployees,name,revenue&$expand=opportunity_parent_account($select=createdon,name),Account_Tasks($select=subject,scheduledstart)";
var req = new XMLHttpRequest();
req.open("GET",organizationUrl + "/api/data/v8.0/" + query, 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) {
        req.onreadystatechange = null;
        if (this.status == 200) {
  
    var RetrievedAccount = JSON.parse(this.response);
    console.log(RetrievedAccount);
          alert(RetrievedAccount.name);
        } else {
            var error = JSON.parse(this.response).error;
            alert(error.message);
        }
    }
};
req.send();
}

Monday 4 January 2016

MS CRM 2016 Web API Operations - Delete a single property value

Delete a single property value:

To delete the value of a single property use a DELETE request with the property name appended to the Uri of the entity.

The following example deletes the value of the fax property of an account entity.
function UpdatAccountSingleValueUsingDELETE() {

debugger;
var clientURL = Xrm.Page.context.getClientUrl();
var AccountId =  "B3A2B300-A8AF-E511-80DD-6C3BE5A878BC";

//Single Property value  which you want to delete
var Property ="fax";

UpdatAccountSingleValue(AccountId,clientURL,Property);


}
 

function UpdatAccountSingleValue(AccountId,clientURL,Property)
{
 
var req = new XMLHttpRequest();
req.open('DELETE', clientURL + "/api/data/v8.0/accounts(" + AccountId + ")/"+Property, true);
req.setRequestHeader("Content-type","application/json");
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) {   
   console.log("Updated account with ID: "+ AccountId)
  }
  else {
   var error = JSON.parse(this.response).error;
   console.log(error.message);
  }
 }
};

req.send();
 

}

MS CRM 2016 Web API Operations - Update a single property value


Update a single property value :

When you want to update only a single property value use a PUT request with the property name appended to the Uri of the entity.

Whenever you want to update single attribute of an entity it can be done by using PUT verb.

Note : while creating object use "value"  property to accommodate the Updated value of attribute.


function UpdatSinglePropertyAccountByPUT() {

debugger;
var clientURL = Xrm.Page.context.getClientUrl();
var AccountId =  "B3A2B300-A8AF-E511-80DD-6C3BE5A878BC";

//Attribute which you want to update e.g Name
var Property ="name";

var AccountTobeUpdated ={};
AccountTobeUpdated["value"] ="My first Update On Account by API using PUT";

UpdatAccountByPUT(AccountId,clientURL,AccountTobeUpdated,Property)


}

 

function UpdatAccountByPUT(AccountId,clientURL,AccountTobeUpdated,Property)
{
 
var req = new XMLHttpRequest();
req.open('PUT', clientURL + "/api/data/v8.0/accounts(" + AccountId + ")/"+Property, true);
req.setRequestHeader("Content-type","application/json");
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) {   
   console.log("Updated account with ID: "+ AccountId)
  }
  else {
   var error = JSON.parse(this.response).error;
   console.log(error.message);
  }
 }
};
req.send(JSON.stringify(AccountTobeUpdated));
 

}