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

How to add an entity to a solution with javascript using Web Api action AddSolutionComponent?

我想使用 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 是否有任何其他解决方案来完成工作?

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

几个变化:

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

  2. 在参数行 'SolutionUniqueName':solutionUniqueName,

  3. 中输入解决方案名称而不是解决方案 ID

  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 中对此进行了测试。