且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Dynamics CRM的组织数据服务与node.js应用程序连接起来

更新时间:2023-01-28 11:39:33

我正在共享this blog中的gihub代码示例。

这使用Node.js脚本中的OrganizationData服务拉取联系人的全名(ContactSet)。

    // Set the headers for the call to CRM
    var headers = {
      'Authorization': 'Bearer ' + sess.access_token, //send the oauth access token to authenticate
      'Accept': 'application/json' //tell CRM to send json data back
    }

    //configure the CRM odata request
    var options = {
      host : crm_host,
      port : crm_port,
      path : '/XRMServices/2011/OrganizationData.svc/ContactSet?$select=FullName', //hardcoded to select just the contact name
      method : 'GET',
      rejectUnauthorized: false,//to allow for self-signed SSL certificates - use at your own risk!!!
      headers : headers //set in the previous step
    };
    
    var reqGet = https.request(options, function(resGet) {
      //should do something here if we get 'www-authenticate': 'Bearer error' response headers
      //console.log("headers: ", resGet.headers);
      
      resGet.on('data', function(d) {
        //console.info('raw response: ' + d);
        var json = JSON.parse(d);
        var records = json.d.results;
        
        //console.info('results: ' + JSON.stringify(records));
        for (var i in records) {   
          res.write(records[i].FullName + '<br />');
        }
        res.write('</body>');
        res.write('</html>');
        res.end();
      });
    });
    reqGet.end();
    
    //handle errors
    reqGet.on('error', function(e) {
      console.error(e);
    });