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)

Access Denied errors using HTTPRequest with CRM365 Online

(0) ShareShare
ReportReport
Posted on by 190

Hi,

I know this is very high level but I am not sure where else to start. We have several HTML web resources that we load in dialogue windows. These work fine with our on prem 365 CRM organisation but when imported to our Online organisation we get 401 and 404 errors when using HTTPRequests to get data. Absolutely nothing was changed with the code so I do not understand why these errors are happening. Of course I suspect it has to do with the difference between on-prem and online but I can't find any info simple enough to explain. 

Any help/advice would be greatly appreciated. 

Thanks

Phil

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Can you please post one of your HTML webresources that executes those requests?

  • fillcrocker Profile Picture
    190 on at

    Hi Andrew, thanks for your reply. The requests are called from a separate JavaScript file (below) and the data is displayed in the HTML web resource.

    The call to this JavaScript file looks like this and it is called from a second JavaScript file on load of the form (initiated by $( document).ready()

    SDK.RestEndpointPaging.RetrieveRecords("/cs_mainactivitySet?$select=cs_mainactivityId,cs_name,cs_IsInScope,cs_Description", mainActivityDataCallback);

    Here is the code for the JavaScript that makes the HTTPRequest. When I debug through the readyState is 'undefined' :

    if (typeof (SDK) == "undefined")

    { SDK = { __namespace: true }; }

    // Namespace container for functions in this library.

    SDK.RestEndpointPaging = {

       GetODataPath: function () {

           /// <summary>

           /// Utility function to retrieve the path to the REST endpoint.

           /// </summary>

          var restEndPoint = "/" + Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc";

          return restEndPoint;

       },

       RetrieveRecords: function (filter, callback) {

           /// <summary>

           /// Initiates an asynchronous request to retrieve records.

           /// If there are additional pages of records the SDK.RestEndpointPaging.RetrieveRecordsCallBack function will

           /// call this function.

           /// </summary>

           var retrieveRecordsReq = new XMLHttpRequest();

           retrieveRecordsReq.open("GET", SDK.RestEndpointPaging.GetODataPath() + filter, true);

           retrieveRecordsReq.setRequestHeader("Accept", "application/json");

           retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           retrieveRecordsReq.onreadystatechange = function () {

               /// <summary>

               /// This event handler passes the callback through

               /// </summary>

               SDK.RestEndpointPaging.RetrieveRecordsCallBack(this, callback);

           };

           retrieveRecordsReq.send();

       },

       RetrieveRecordsCallBack: function (retrieveRecordsReq, callback) {

           /// <summary>

           /// Handles the onreadystatechange event to process the records returned.

           /// If more pages are available this function will call the SDK.RestEndpointPaging.RetrieveRecords

           /// function to get the rest.

           /// </summary>

           if (retrieveRecordsReq.readyState == 4 /* complete */) {

               if (retrieveRecordsReq.status == 200) {

                   var retrievedRecords = JSON.parse(retrieveRecordsReq.responseText).d;

                   /// The callback is called with the results.

                   callback(retrievedRecords.results);

                   if (null != retrievedRecords.__next) {

                       // The existance of the '__next' property indicates that more records are available

                       // So the originating function is called again using the filter value returned

                       //var filter = retrievedRecords.__next.replace(SDK.RestEndpointPaging.GetODataPath(), "");

                       var filter = retrievedRecords.__next.substr(retrievedRecords.__next.indexOf(".svc/")+4 , retrievedRecords.__next.length);

                       SDK.RestEndpointPaging.RetrieveRecords(filter, callback);

                   }

               }

               else {

                   //Failure

                   SDK.RestEndpointPaging.errorHandler(retrieveRecordsReq);

               }

           }

       },

       //Function to handle any http errors

       errorHandler: function (XmlHttpRequest) {

           /// <summary>

           /// Simply displays an alert message with details about any errors.

           /// </summary>

           alert("Error : " +

           XmlHttpRequest.status + ": " +

           XmlHttpRequest.statusText + ": " +

           JSON.parse(XmlHttpRequest.responseText).error.message.value);

       },

       __namespace: true

    };

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Following line

    var restEndPoint = "/" + Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc";

    has couple of issues.

    Try to use

    var restEndPoint = GetGlobalContext().getClientUrl() + "/XRMServices/2011/OrganizationData.svc";

    instead. To make GetGlobalContext function available you should add reference to your html file according to following article - msdn.microsoft.com/.../gg328541.aspx

  • fillcrocker Profile Picture
    190 on at

    Hi Andrew, the HTML file does not contain the HTTPRequest. The request is within a JavaScript file. Can I use GetGlobalContext() in a JavaScript file. I can only see info on how to reference it in a HTML file.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I believe that your JS file is references in your HTML. Reference GlobalContext in the same HTML file to use GetGlobalContext function in your JS. Good luck.

  • fillcrocker Profile Picture
    190 on at

    That worked. You sir are a Genius!

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Then may be mark replies that helped you as answers and not yours? :)

  • fillcrocker Profile Picture
    190 on at

    Thanks for your help. It is very much appreciated!

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