locked
CRM 2011 Online Prompting Credentials Entry for Odata Query RRS feed

  • Question

  • Hello all,

    I'm in the process below of trying to learn how to use Odata by making a simple request for data from another entity within my CRM 2011 online environment. I have two problems though they may be related.

    1. When I run my below code, as soon as the XMLHttpRequest retrieveReq uses its send() function, internet explorer (and also Firefox and Chrome) prompt me for a username and password. Given I'm already logged in I don't understand why this would happen and I can't seem to find any articles that explain how to fix this.

    2. My second problem is that if I simply click cancel or enter some user information to get past the credentials prompt, I then get the javascript error " Invalid character, character 9" on the line  "var retrieved = JSON.parse(retrieveReq.responseText).d;" in the below solution which again I don't understand why. 

    Also the function tryRest() below is triggered when I select a value from a lookup field "new_certificate".

    I'm quite new to this stuff so all your help is appreciated. Thanks in advance! 

    function tryRest() 
    {	
        var serverUrl = document.location.protocol + "//" + document.location.host + "/XRMServices/2011/OrganizationData.svc";
        var newCertificateid = Xrm.Page.getAttribute("new_certificate").getValue();
        var newCertificateid = primarycontact[0].id;
    	var newCertificateid = newCertificateid.replace('{', '').replace('}', '').toLowerCase();
    
     // Creating the Odata Endpoint
        var oDataPath = serverUrl 
        var retrieveReq = new XMLHttpRequest();
    	var Odata = oDataPath +   "/new_certificateSet$select=new_CertificateClassSUFFOLk&$filter=new_certificateId eq guid'"+newCertificateid+"'";
    
        retrieveReq.open("GET", Odata, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        retrieveReq.onreadystatechange = function () { retrieveReqCallBack(this); };
        retrieveReq.send();
    }
    
    function retrieveReqCallBack(retrieveReq) 
    {
        if (retrieveReq.readyState == 4 /* complete */) 
    	{
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            var output = retrieved.results;
    		// Alert the output from new_certificate object. Note I've yet to configure this - the program won't get here currently. 
            alert(output);
         }
    }

    Monday, August 19, 2013 4:02 PM

All replies

  • Debug the script you will understand wat prob in that..

    ms crm

    Tuesday, August 20, 2013 7:22 AM
  • How are you calling the code - is it from within a CRM web resource, or a page hosted outside of CRM ? If you're within a CRM web resource, then using the getClientUrl function to build the url should resolve the login prompt.

    This may fix the other problem - if you don't get authenticated, you'd get an HTTP error, which the JSON.parse function will not be able to parse


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, August 20, 2013 8:22 AM
    Moderator
  • Hi David,

    Thanks for your response. Yes I am calling the code from a JavaScript web resource within CRM so I tried modifying my server URL per your above suggestion so that line is now as follows.

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

    Unfortunately this doesn't solve the problem and I still get prompted to login. If you have any other ideas please let me know.

    Thanks

    Tuesday, August 20, 2013 9:15 AM
  • Solved this problem by scrapping the above example and using the following code: 

    function tryRest()
    {
        var lookUpObjectValue = Xrm.Page.getAttribute("new_certificate").getValue();
        if ((lookUpObjectValue != null))
        {
     
            var lookupid = lookUpObjectValue[0].id;
            alert(lookupid);
    		var lookupid = lookupid.replace('{', '').replace('}', '').toLowerCase();
    		alert(lookupid);
     
     
    		var serverUrl = Xrm.Page.context.getClientUrl();
     
    		//The XRM OData end-point
    		var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
     
     
    		var odataSetName = "new_certificateSet";
     
    		var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + lookupid + "')";
     
        //alert(odataSelect);
     
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
     
                var result_contact= data.d;
               
                alert(result_contact.new_CertificateClassSUFFOLk);
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
        });
     
        }
     
      }

    Thanks everyone

    Tuesday, August 20, 2013 12:59 PM