如何使用Web Api操作AddSolutionComponent使用javascript将实体添加到解决方案中?

时间:2019-04-12 14:37:09

标签: javascript xmlhttprequest dynamics-crm dynamics-365 dynamics-crm-webapi

我想使用JavaScript将自定义实体添加到Dynamics CRM中的自定义解决方案中。 我已经进行了一些研究,结果发现可以使用Actions来完成。 AddSolutionComponent应该可以完成这项工作,但是由于遇到错误400 Request message has unresolved parameters,我可能会出错。

我要传递参数的实体和解决方案都使用javascript创建,并且可以在crm中找到它们。

function associateEntityToSolution(entityId, solutionUniqueName, newSolutionId){

  var param = { 
      'ComponentId': entityId , // newly created entity id 
      'ComponentType':1, // entity type
      'SolutionUniqueName':solutionUniqueName,  //newly created solution id
      'AddRequiredComponents':false,
      'IncludedComponentSettingsValues':null
  };

  var req = new XMLHttpRequest();
  req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/solutions("+newSolutionId+")/Microsoft.Dynamics.CRM.AddSolutionComponent", true);
  req.setRequestHeader("OData-MaxVersion", "4.0");
  req.setRequestHeader("OData-Version", "4.0");
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.onreadystatechange = function() {
      if (this.readyState === 4) {
          req.onreadystatechange = null;
          if (this.status === 204) {
              var uri = this.getResponseHeader("OData-EntityId");
              var regExp = /\(([^)]+)\)/;
              var matches = regExp.exec(uri);
              var newEntityId = matches[1];
              associateEntityToSolution(newEntityId,entityUniqueName);
          } else {
              window.parent.opener.Xrm.Utility.alertDialog(this.statusText);
          }
      }
  };
  req.send(JSON.stringify(param));
}

我在代码中缺少什么吗? 还有其他解决方案可以使用javascript完成工作吗?

2 个答案:

答案 0 :(得分:1)

变更夫妇:

  1. 评论了这一行associateEntityToSolution(newEntityId,entityUniqueName);,因为我猜想这可能会循环。

  2. 将解决方案名称而非解决方案ID放在参数行'SolutionUniqueName':solutionUniqueName,

enter image description here

  1. 将此行req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/solutions("+newSolutionId+")/Microsoft.Dynamics.CRM.AddSolutionComponent", true); 更改为正确的Action Web api调用,如下所示:req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v9.1/AddSolutionComponent", true);

-

function associateEntityToSolution(entityId, solutionUniqueName, newSolutionId){

  var param = { 
      'ComponentId': entityId , // newly created entity id 
      'ComponentType':1, // entity type
      'SolutionUniqueName':solutionUniqueName,  // solution name (without spaces)
      'AddRequiredComponents':false,
      'IncludedComponentSettingsValues':null
  };

  var req = new XMLHttpRequest();
  req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v9.1/AddSolutionComponent", true);
  req.setRequestHeader("OData-MaxVersion", "4.0");
  req.setRequestHeader("OData-Version", "4.0");
  req.setRequestHeader("Accept", "application/json");
  req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  req.onreadystatechange = function() {
      if (this.readyState === 4) {
          req.onreadystatechange = null;
          if (this.status === 204) {
              var uri = this.getResponseHeader("OData-EntityId");
              var regExp = /\(([^)]+)\)/;
              var matches = regExp.exec(uri);
              var newEntityId = matches[1];
              //associateEntityToSolution(newEntityId,entityUniqueName);
          } else {
              window.parent.opener.Xrm.Utility.alertDialog(this.statusText);
          }
      }
  };
  req.send(JSON.stringify(param));
}

我在CRM REST Builder中对此进行了测试。

答案 1 :(得分:0)

URL:

POST [Your Org]/api/data/v9.0/AddSolutionComponent

身体:

{
    "ComponentId" : "YourComponentGuid",
    "ComponentType" : "YourComponentType", 
    "SolutionUniqueName" : "YourSolutionUniqueName",
    "AddRequiredComponents" : "false", //false or true
    "DoNotIncludeSubcomponents" : "true" //false or true
}

可以通过对[Your Org]/api/data/v9.0/EntityDefinitions(LogicalName='YourEntityLogicalName')?$select=MetadataId进行GET请求来检索ComponentId。

搜索可用的组件类型here