Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Calling the WebApi from a javascript webresource in Dynamics CRM 2016 on Premise

(0) ShareShare
ReportReport
Posted on by

this is my snippet of code. when I try to test it shows me a Popup asking for credentials. while debuging it seems access is denied to that Url (I(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/salesorders(" + nameauftrag + "))

---------------------------------------------------

var nameauftrag  = Xrm.Page.data.entity.getId().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');

quntity = 0;

var req = new XMLHttpRequest();
req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/salesorders(" + nameauftrag + ")?$expand=order_details($select=quantity)"), 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function ()
{
if (this.readyState === 4)
{
req.onreadystatechange = null;
if (this.status === 200)
{
var result = JSON.parse(this.response);
for (var i = 0; i < result.value.length; i++)
{
quantity= quantity + result[i].value.order_details["quantity"];
}
}
else
{
//alert("An error occurs: Status code: " + this.statusText);
}
}
};
req.send();

*This post is locked for comments

  • RE: Calling the WebApi from a javascript webresource in Dynamics CRM 2016 on Premise

    Hi Michel, Thank you for your response. I already resolved the Problem. using Fiddler I copy the Url of the calling Webapi and it seems in the filter I Forget the question mark.

  • RE: Calling the WebApi from a javascript webresource in Dynamics CRM 2016 on Premise

    [quote user="Labib"]

    this is my snippet of code. when I try to test it shows me a Popup asking for credentials. while debuging it seems access is denied to that Url (I(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/salesorders(" + nameauftrag + "))

    ---------------------------------------------------

    var nameauftrag  = Xrm.Page.data.entity.getId().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');

    quntity = 0;

    var req = new XMLHttpRequest();
    req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/salesorders(" + nameauftrag + ")?$expand=order_details($select=quantity)"), 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function ()
    {
    if (this.readyState === 4)
    {
    req.onreadystatechange = null;
    if (this.status === 200)
    {
    var result = JSON.parse(this.response);
    for (var i = 0; i < result.value.length; i++)
    {
    quantity= quantity + result[i].value.order_details["quantity"];
    }
    }
    else
    {
    //alert("An error occurs: Status code: " + this.statusText);
    }
    }
    };
    req.send();

    [/quote][quote user="Labib"]

    this is my snippet of code. when I try to test it shows me a Popup asking for credentials. while debuging it seems access is denied to that Url (I(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/salesorders(" + nameauftrag + "))

    ---------------------------------------------------

    var nameauftrag  = Xrm.Page.data.entity.getId().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');

    quntity = 0;

    var req = new XMLHttpRequest();
    req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/salesorders(" + nameauftrag + ")?$expand=order_details($select=quantity)"), 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function ()
    {
    if (this.readyState === 4)
    {
    req.onreadystatechange = null;
    if (this.status === 200)
    {
    var result = JSON.parse(this.response);
    for (var i = 0; i < result.value.length; i++)
    {
    quantity= quantity + result[i].value.order_details["quantity"];
    }
    }
    else
    {
    //alert("An error occurs: Status code: " + this.statusText);
    }
    }
    };
    req.send();

    [/quote]

  • Suggested answer
    Michel Gueli Profile Picture
    Michel Gueli 982 on at
    RE: Calling the WebApi from a javascript webresource in Dynamics CRM 2016 on Premise

    Is the result from Xrm.Page.context.getClientUrl() different than the URL in your address bar? If the answer is yes. Check the deployment manager: technet.microsoft.com/.../dn920275.aspx

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans