Dynamics CRM2016 Web API之Use custom FetchXML

      CRM2016中新增的web api支持fetch xml了,之前使用FetchXML的场景是在后天代码中通过组织服务的retrieve multiple方法,但实际的应用效果有多大,还需要在实际的项目中去体会了。

    上代码,注意要对fetch xml进行编码

 var fetch="<fetch mapping='logical'>" 
           +"<entity name='account'>"
           +"<attribute name='accountid'/> "
           + "<attribute name='name'/> "
            + "<attribute name='primarycontactid'/> "
           +"</entity>"
           + "</fetch>"
  var req = new XMLHttpRequest()
  req.open("get", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/accounts?fetchXml="+ encodeURI(fetch), false);
  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) {
          if (this.status == 200) {
              var data = JSON.parse(this.responseText);
          }
          else {
              var data = JSON.parse(this.responseText).error.message;
          }
      }
  };
  req.send();

下图是查询出的结果



posted on 2016-04-01 14:18  代码王子  阅读(453)  评论(0编辑  收藏  举报

导航