web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Calling action through JS

(0) ShareShare
ReportReport
Posted on by 2,003

Hi

While calling an action we are supposed to make a call to Soap service.

msdn.microsoft.com/.../gg594434.aspx

function GetClientUrl() {
    if (typeof Xrm.Page.context == "object") {
        clientUrl = Xrm.Page.context.getClientUrl();
    }
    var ServicePath = "/XRMServices/2011/Organization.svc/web";
    return clientUrl + ServicePath;
}

 var req = new XMLHttpRequest();
    req.open("POST", GetClientUrl(), false)
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");
    req.send(requestXML);

when i open a Soap URL in browser(receive from function GetClientUrl) i get an error Htpp 400- bad request. Which Url need to used to make soap call.

Thanks.

*This post is locked for comments

I have the same question (0)
  • Daniel Wikell Profile Picture
    2,360 on at

    "Http 400 Bad request" means that the url was found, but something in the request was malformed.

    What is the content of the requestXML variable that you are sending in? There's likely something in there that's not properly formatted.

  • Abhishek Gupta Profile Picture
    2,003 on at

    hi, thanks for reply. this is my complete function

    function ExecuteActionCreateProject(entityId, entityName, requestName) {

       // Creating the request XML for calling the Action

       var requestXML = ""

       requestXML += "<s:envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

       requestXML += "  <s:body>";

       requestXML += "    <execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

       requestXML += "      <request xmlns:a=\"schemas.microsoft.com/.../Contracts\">";

       requestXML += "        <a:parameters xmlns:b=\"schemas.datacontract.org/.../System.Collections.Generic\">";

       requestXML += "          <a:keyvaluepairofstringanytype>";

       requestXML += "            <b:key>Target</b:key>";

       requestXML += "            <b:value i:type=\"a:EntityReference\">";

       requestXML += "              <a:id>" + entityId + "</a:id>";

       requestXML += "              <a:logicalname>" + entityName + "</a:logicalname>";

       requestXML += "              <a:name i:nil=\"true\">";

       requestXML += "            </a:name></b:value>";

       requestXML += "          </a:keyvaluepairofstringanytype>";

       requestXML += "        </a:parameters>";

       requestXML += "        <a:requestid i:nil=\"true\">";

       requestXML += "        <a:requestname>" + requestName + "</a:requestname>";

       requestXML += "      </a:requestid></request>";

       requestXML += "    </execute>";

       requestXML += "  </s:body>";

       requestXML += "</s:envelope>";

       var req = new XMLHttpRequest();

       req.open("POST", GetClientUrl(), false)

       req.setRequestHeader("Accept", "application/xml, text/xml, */*");

       req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

       req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

       req.send(requestXML);

       //Get the Response from the CRM Execute method

       //var response = req.responseXML.xml;

       req.onerror = function (e) {

           alert(req.statusText);

       };

       if (req.status === 200) {

           alert(req.responseText);

       }

    }

    which is throwing me an error of 404 bad request.

  • Daniel Wikell Profile Picture
    2,360 on at

    For this line:

    req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&amp;quot);

    Is the " part of your code or was it misformatted when you pasted it in here?

    If it's also in your code then it should be replaced by a regular " quotation mark, otherwise I don't believe it will be interpreted right.

  • Daniel Wikell Profile Picture
    2,360 on at

    oh wow, this board totally butcheted that. It should say

    is the "& quot ;" part of your code

  • Abhishek Gupta Profile Picture
    2,003 on at

    Hi it is formed during pasting over here otherwise req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");

  • Daniel Wikell Profile Picture
    2,360 on at

    Alright

    Have you checked the formatting for the entityId and entityName parameters?

    The SOAP-services usually require the entityname to be in all lower case for instance (as opposed to the REST-services that requires them to be cased according to the schema-name). Not sure if this endpoint accepts an entityId guid that is enclosed with the brackets either. You could try strip the { } curly braces from the entityId-string if that is the case..

  • Abhishek Gupta Profile Picture
    2,003 on at

    Hi i have tested that before. My entity name is in lower case and i have tried removing the braces around the ID. Issue still remain the same.

  • Verified answer
    Abhishek Gupta Profile Picture
    2,003 on at

    The issue was with Soap Envelope, which goes like this now after rectifying it.

    function ExecuteActionCreateProject(EntityId,entityId, entityName, requestName) {

       // Creating the request XML for calling the Action

       var requestXML = ""

       requestXML += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

       requestXML += "  <s:Body>";

       requestXML += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

       requestXML += "      <request xmlns:a=\"schemas.microsoft.com/.../Contracts\">";

       requestXML += "        <a:Parameters xmlns:b=\"schemas.datacontract.org/.../System.Collections.Generic\">";

       requestXML += "          <a:KeyValuePairOfstringanyType>";

       requestXML += "            <b:key>Target</b:key>";

       requestXML += "            <b:value i:type=\"a:EntityReference\">";

       requestXML += "              <a:Id>"+entityId+"</a:Id>";

       requestXML += "              <a:LogicalName>"+entityName+"</a:LogicalName>";

       requestXML += "              <a:Name i:nil=\"true\">";

       requestXML += "            </a:Name></b:value>";

       requestXML += "          </a:KeyValuePairOfstringanyType>";

       requestXML += "          <a:KeyValuePairOfstringanyType>";

       requestXML += "            <b:key>EntityId</b:key>";

       requestXML += "            <b:value i:type=\"c:string\" xmlns:c=\"www.w3.org/.../XMLSchema\">"+EntityId+"</b:value>";

       requestXML += "          </a:KeyValuePairOfstringanyType>";

       requestXML += "        </a:Parameters>";

       requestXML += "        <a:RequestId i:nil=\"true\">";

       requestXML += "         </a:RequestId>";

       requestXML += "        <a:RequestName>"+requestName+"</a:RequestName>";

       requestXML += "      </request>";

       requestXML += "    </Execute>";

       requestXML += "  </s:Body>";

       requestXML += "</s:Envelope>";

       var req = new XMLHttpRequest();

       req.open("POST", GetClientUrl(), false)

       req.setRequestHeader("Accept", "application/xml, text/xml, */*");

       req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

       req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

       req.send(requestXML);

       //Get the Response from the CRM Execute method

       //var response = req.responseXML.xml;

       }

    }

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans