Search code examples
javascriptdynamics-crmodatadynamics-365

"Resource not found for the segment *publisherguid*" in XMLHTTP query to create solution in the CRM


I'm trying to create a solution in the CRM with javascript. My code is a webresource that I get through a Ribbon I created with "Ribbon Workbench 2016". These things work very good, but when I try to pass the data I get from user ( from a form ) to the CRM, I get the error in the title.

At first I thought the problem was that the guid was all in lower case, so I converted it in upper case. Nothing changed. Then I tried using the friendlyname of the publisher instead of the guid. Nothing changed. Al last, I was frustrated, so I used an empty string, and the error changed from the one in the title, to "linkPath should have 2 segments". Guess it was a progress...but still have no idea what the real error might be.

What am I doing wrong? Is it right to treat the solution as an entity an create it that way? Is there a better way?

PS: The query was generated with CRM Rest Builder

var entity = {};
entity.friendlyname = $("#solutionForm").dxForm("instance").getEditor("Friendly name").option("value");
entity.uniquename = $("#solutionForm").dxForm("instance").getEditor("Unique name").option("value");
entity.version = $("#solutionForm").dxForm("instance").getEditor("Version").option("value");
entity["[email protected]"] = keyValueContainerForPublishers[($("#solutionForm").dxForm("instance").getEditor("Publisher").option("value"))]; //contains guid of selected publisher
entity["[email protected]"] = "";
entity.description = $("#solutionForm").dxForm("instance").getEditor("Description").option("value");
entity.solutionid = newGuid(); //create unique guid
entity.solutionpackageversion = null;
entity.solutiontype = 0;

var req = new XMLHttpRequest();
req.open("POST", window.parent.opener.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/solutions", 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];
         } else {
             window.parent.opener.Xrm.Utility.alertDialog(this.statusText);
         }
      }
};
req.send(JSON.stringify(entity));

Solution

  • Alright below Webapi query I tried for creating solutiona and it did worked form me.

    Few points to take care of

    1. version should be someting like 1.0 or 2.0 or so. only 1 or 2 will not work
    2. Publisher, If you compare your code and my code it should be "publishers" and not "publisher"
    3. SolutionID you don't have to mention it will automatically create
    4. I did not used configuraitonPageID and solutionPackageVersion for now.

    Taking care of above things did created a solution for me.

    var entity = {};
    entity.friendlyname = "Test solution from WebAPI";
    entity.uniquename = "TestSolutionFromWebAPI";
    entity.version = "1.0";
    entity["[email protected]"] = "/publishers(6007BA03-EE7A-4CA1-A146-7EB0044E504F)";
    entity.description = "This is test solution form webapi";
    entity.solutiontype = 0;
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/solutions", false);
    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];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));