Dynamics 365 and Node.js integration using the Web API - part 2

Last year I wrote a post that showed how to retrieve data from a Dynamics 365 Online organization in a Node.js application using the Web API. Today I will share sample code that shows how to update data from a Node.js application using the Web API.

Updating a single property

To update a single property on a record in Dynamics 365, you can make a PUT request to the Web API. The raw HTTP request to update the first name for a contact would look like this:

PUT [Organization URI]/api/data/v8.2/contacts(00000000-0000-0000-0000-000000000001)/firstname HTTP/1.1
Content-Type: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0

{"value": "Demo-Firstname"}

Assuming you have retrieved the OAuth token for authenticating to Dynamics 365 as I outlined in my earlier blog post, here is a sample Node function to make a PUT update request:

function updateContactPut(token, contactid){
	var contactObj={};
	contactObj["value"]="Firstname PUT";
	var requestdata = JSON.stringify(contactObj);
	var contentlength = Buffer.byteLength(JSON.stringify(contactObj));

    //set the crm request parameters and headers
    var crmrequestoptions = {
		path: '/api/data/v8.2/contacts('+contactid+')/firstname',
        host: crmwebapihost,
        method: 'PUT',
        headers: { 
			'Authorization': 'Bearer ' + token,
			'Content-Type': 'application/json',
			'Content-Length': contentlength,
			'OData-MaxVersion': '4.0',
			'OData-Version': '4.0'
		}
    };
	
	//make the web api request
    var crmrequest = https.request(crmrequestoptions, function(response) {
        //make an array to hold the response parts if we get multiple parts
        var responseparts = [];
        //response.setEncoding('utf8');
        response.on('data', function(chunk) {
            //add each response chunk to the responseparts array for later
            responseparts.push(chunk);      
        });
        response.on('end', function(){
            //once we have all the response parts, concatenate the parts into a single string - response should be empty for this, though
            var completeresponse = responseparts.join('');
			console.log(completeresponse);
			console.log("success");
        });
    });
    crmrequest.on('error', function(e) {
        console.error(e);
    });

	//send the data to update
	crmrequest.write(requestdata);
	
    //close the web api request
    crmrequest.end();
}

Although the content-length header is technically not required, the Node HTTPS module will not send the data to the Web API with the correct encoding unless you set it in your request.

Updating multiple properties

To update a multiple properties on a record in Dynamics 365, you must make a PATCH request to the Web API. The raw HTTP request to update the first name and last name for a contact would look like this:

PUT [Organization URI]/api/data/v8.2/contacts(00000000-0000-0000-0000-000000000001) HTTP/1.1
Content-Type: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0

{
"firstname": "Demo-Firstname",
"lastname": "Demo-Lastname"
}

Again, assuming you have retrieved the OAuth token for authenticating to Dynamics 365 as I outlined in my earlier blog post, here is a sample Node function to make a PATCH update request:

function updateContactPatch(token, contactid){
	var contactObj={};
	contactObj["firstname"]="Firstname test";
	contactObj["lastname"]="Lastname test";
	var requestdata = JSON.stringify(contactObj);
	var contentlength = Buffer.byteLength(JSON.stringify(contactObj));

    //set the crm request parameters and headers
    var crmrequestoptions = {
		path: '/api/data/v8.2/contacts('+contactid+')',
        host: crmwebapihost,
        method: 'PATCH',
        headers: { 
			'Authorization': 'Bearer ' + token,
			'Content-Type': 'application/json',
			'Content-Length': contentlength,
			'OData-MaxVersion': '4.0',
			'OData-Version': '4.0'
		}
    };
	
	//make the web api request
    var crmrequest = https.request(crmrequestoptions, function(response) {
        //make an array to hold the response parts if we get multiple parts
        var responseparts = [];
        //response.setEncoding('utf8');
        response.on('data', function(chunk) {
            //add each response chunk to the responseparts array for later
            responseparts.push(chunk);      
        });
        response.on('end', function(){
            //once we have all the response parts, concatenate the parts into a single string - response should be empty for this, though
            var completeresponse = responseparts.join('');
			console.log(completeresponse);
			console.log("success");
        });
    });
    crmrequest.on('error', function(e) {
        console.error(e);
    });

	//send the data to update
	crmrequest.write(requestdata);
	
    //close the web api request
    crmrequest.end();
}

As with the earlier PUT sample, the content-length header is technically not required, but the Node HTTPS module will not send the data to the Web API with the correct encoding unless you set it in your request.

Further reading

For more information on updating and deleting data with the Dynamics 365 Web API, take a look at the "Update and delete entities using the Web API" article on MSDN.

comments powered by Disqus