Showing posts with label Dynamics CRM Integration. Show all posts
Showing posts with label Dynamics CRM Integration. Show all posts

Saturday, July 6, 2019

Integrate Dynamics CRM 365 with Node Js Application


In recent days Node Js becoming quite popular. Different kind of portals and applications are made on Node Js technology because of it's performance and efficiency.

So it's becoming important to integrated Dynamics CRM 365 with node JS.

In this blog I will try to explain how we can integrate Dynamics 365 to Node Js with help of Azure Apps.

After creating azure app we can integrate Dynamics 365 with our other products also.

There are two kind of Azure App which can be used for integration.

1. Native App
2. Web App/ API Application

We can use Both type of application in integration. But the major difference is:-
1. We need to use User name and Password while integrate via Native APP.

2.  We need to use secret key and application user while integrate via Web App /API APP.

Native APP Integration:-

1. Login in Azure with admin permission

2. Click on “Azure Active Directory”

























3. You will find two App registration
      a) App Registrations.
      b) App Registrations(Legacy)


















4.       Click on App registration not Legacy.

5.       Here we will find “Owned applications” and “All applications”, where we can see all registered Apps.












6. Click on “New registration” and select below options.




























Click on “Register” button.

7. Copy and Save Application (client) ID for further use.
















8. Click on Authentication and made below changes and save.












9. Click on API permission and add Dynamics CRM Online.





























































10. Grant admin consent






















11. Go back to App registration and select Endpoints.
and copy Token endpoint(v1)

































If you still want to create Legacy APP, select App registration(Legacy)

1. Provide information and select Application Type as “Native”.




















2. Copy Application ID to use as Client Id while integration.
3.  Click on settings and select “Required Permissions”.



























4. Add Dynamics CRM Online in permission.












We can also use Web App /API APP for Integration


1. With new App you can use same App for Native and Web API as both.

2.  Go to Authentication and provide Type as Web.












3. Add secret Key and create Application User .













Copy and Save secret key for further use.






























1. (Web App with Legacy App Registration) Provide information and select Application Type “Web App/ API”.



















2. Click on settings and select “Key”.

























We can choose one of four (Legacy Native App, Legacy Web App, New Native App, New Web App)
for integration.

Now our App ready to integrate for other product like Node.Js

1. To test application Node.js online, open













and paste below code. Update organization and application information in Code.

//Use Below Sample Code started For Native App Integration Testing
'use strict';
var https = require('https');

//set these values to retrieve the oauth token
var crmorg = 'https://CRMORG.crm8.dynamics.com';
//Application ID
var clientid = 'Application ID';
var username = 'User Name';
var userpassword = 'Password';
var tokenendpoint = 'Pasted copied Endpoint';
var crmwebapihost = ' CRMORG.api.crm8.dynamics.com';
var crmwebapipath = '/api/data/v9.1/contacts'; //basic query to select contacts
//remove https from tokenendpoint url
tokenendpoint = tokenendpoint.toLowerCase().replace('https://', '');
//get the authorization endpoint host name
var authhost = tokenendpoint.split('/')[0];
//get the authorization endpoint path
var authpath = '/' + tokenendpoint.split('/').slice(1).join('/');
//build the authorization request
var reqstring = 'client_id=' + clientid;
reqstring += '&resource=' + encodeURIComponent(crmorg);
reqstring += '&username=' + encodeURIComponent(username);
reqstring += '&password=' + encodeURIComponent(userpassword);
reqstring += '&grant_type=password';
//set the token request parameters
var tokenrequestoptions = {
    host: authhost,
    path: authpath,
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(reqstring)
    }
};
//make the token request
var tokenrequest = https.request(tokenrequestoptions, 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
        var completeresponse = responseparts.join('');
        //console.log('Response: ' + completeresponse);
        console.log('Token response retrieved . . . ');
        //parse the response JSON
        var tokenresponse = JSON.parse(completeresponse);
        //extract the token
        var token = tokenresponse.access_token;
        //pass the token to our data retrieval function
        getData(token);
    });
});
tokenrequest.on('error', function (e) {
    console.error(e);
});
//post the token request data
tokenrequest.write(reqstring);
//close the token request
tokenrequest.end();
function getData(token) {
    //set the web api request headers
    var requestheaders = {
        'Authorization': 'Bearer ' + token,
        'OData-MaxVersion': '4.0',
        'OData-Version': '4.0',
        'Accept': 'application/json',
        'Content-Type': 'application/json; odata.metadata=minimal',
        'Prefer': 'odata.maxpagesize=500',
         'Prefer': 'odata.include-annotations=*'
    };
    //set the crm request parameters
    var crmrequestoptions = {
        host: crmwebapihost,
        path: crmwebapipath,
        method: 'Get',
        headers: requestheaders,
    };
    //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
            var completeresponse = responseparts.join('');
           // console.log(completeresponse);
            //parse the response JSON
            var collection = JSON.parse(completeresponse).value;
collection.forEach(function (row, i) {
  console.log(row['fullname']);
  });
            //loop through the results and write out the fullname
        });
    });
    crmrequest.on('error', function (e) {
        console.error(e);
    });
    //close the web api request
    crmrequest.end();
}
//Use Below Sample Code started For WEB App Integration Testing
'use strict';
var https = require('https');

//set these values to retrieve the oauth token
var crmorg = 'https://CRMORG.crm8.dynamics.com';
//Application ID
var clientid = 'Application/Client ID';
var Client_secret = 'Copied Key';

var tokenendpoint = 'Copied endpoint';

//set these values to query your crm data
var crmwebapihost = 'CRMorg.api.crm8.dynamics.com';
var crmwebapipath = '/api/data/v9.1/contacts'; //basic query to select contacts

//remove https from tokenendpoint url
tokenendpoint = tokenendpoint.toLowerCase().replace('https://', '');

//get the authorization endpoint host name
var authhost = tokenendpoint.split('/')[0];

//get the authorization endpoint path
var authpath = '/' + tokenendpoint.split('/').slice(1).join('/');

//build the authorization request
//if you want to learn more about how tokens work, see IETF RFC 6749 - https://tools.ietf.org/html/rfc6749
var reqstring = 'client_id=' + clientid;
reqstring += '&resource=' + encodeURIComponent(crmorg);
reqstring += '&client_secret=' + Client_secret;
reqstring += '&grant_type=client_credentials';


//set the token request parameters
var tokenrequestoptions = {
    host: authhost,
    path: authpath,
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(reqstring)
    }
};

//make the token request
var tokenrequest = https.request(tokenrequestoptions, 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
        var completeresponse = responseparts.join('');
        //console.log('Response: ' + completeresponse);
        console.log('Token response retrieved . . . ');

        //parse the response JSON
        var tokenresponse = JSON.parse(completeresponse);

        //extract the token
        var token = tokenresponse.access_token;
console.log(token);
        //pass the token to our data retrieval function
        getData(token);
    });
});
tokenrequest.on('error', function (e) {
    console.error(e);
});

//post the token request data
tokenrequest.write(reqstring);

//close the token request
tokenrequest.end();


function getData(token) {
 

 
    //set the web api request headers
    var requestheaders = {
        'Authorization': 'Bearer ' + token,
        'OData-MaxVersion': '4.0',
        'OData-Version': '4.0',
        'Accept': 'application/json',
        'Content-Type': 'application/json; odata.metadata=minimal',
      
        'Prefer': 'odata.maxpagesize=500',
       
         'Prefer': 'odata.include-annotations=*'
      
    };

    //set the crm request parameters
    var crmrequestoptions = {
        host: crmwebapihost,
        path: crmwebapipath,
        method: 'Get',
        headers: requestheaders,
      

    };

    //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
            var completeresponse = responseparts.join('');
           // console.log(completeresponse);
            //parse the response JSON
            var collection = JSON.parse(completeresponse).value;
collection.forEach(function (row, i) {
  console.log(row['fullname']);
  });

 
            //loop through the results and write out the fullname

        });
    });
    crmrequest.on('error', function (e) {
        console.error(e);
    });
    //close the web api request
    crmrequest.end();
}



For Fetch query use below code
var fetchQuery = "encoded fetch query";
var crmwebapipath = '/api/data/v9.1/dynamicproperties?fetchXml=' + fetchQuery;

Wednesday, July 3, 2019

How to use post man with Dynamics CRM 365

With Dynamics 365, Web API use become quite popular and also Microsoft promoting Web API more than other CRUD operation method.
So, sometime we need to execute our web API queries to test or build in JS or HTML web resource or other places.

For this Postman is a great tool to execute or Test our Web API.

In this blog I will try to explain how we can use Postman with Dynamics CRM 365.

 1. Download and Install Postman in your local system. (Download)

 

2. Open Postman and create click on environment to create new environment.














3. Enter Name and add variables to use in later queries.



















Sample Data:-
Name- Demo CRM

Variable
Initial Value
Current Value
url
https://crmorg.crm8.dynamics.com
https://crmorg.crm8.dynamics.com
version
9.1
9.1
webapiurl
{{url}}/api/data/v{{version}}/
{{url}}/api/data/v{{version}}/
Token
Enter token value
Enter token value

4. To get Token Open (http://xrm.tools/) and Sign In with CRM credentials. You also need to accept Consent.

5. Click on "Access Token" link from right side menu.

 Select your organization and click on "Copy to clipboard".









Add copied Token in Postman your environment variable(Token).

6. Create New Collection.





























Go to “Authorization” tab.
Select Type->Bearer Token
Enter {{Token}} variable in token.




6. Create New Request and Select your collection.



    Below are the request we can use in Postman.

1. Retrieve Multiple:-






Select “Get” method.

        Enter “{{webapiurl}}/entity plural name” and click on Send button.

2. Using FetchXML



Select “Get” method.
       Enter “{{webapiurl}}/entity plural name”fetchXml=your fetchxml query” and click on Send button.

3. Delete Record



Select “DELETE” method.
      Enter “{{webapiurl}}/entity plural name(Entity record Guid)” and click on Send button.    


4. Update










Select “PATCH” method.
Enter {{webapiurl}}/entity plural name(Entity record Guid)”

Select Body Tab(Json(application/json)
Enter your information. 
       Click on Send button

5. Create


Select “POST” method.
Enter “{{webapiurl}}/entity plural name”
Select Body Tab(Json(application/json)
Enter your information. 
       Click on Send button.

You can use below reference to get more information on Postman operations:-



Share your feedback and follow blog to be updated !!















Print Preview in Dynamics CE

After moving to new unified interface from legacy web interface. We are moving one important feature Print Preview.  With print Preview we w...