locked
how to get authenticationHeader in crm 2013 RRS feed

  • Question

  • Hi,

    When I run this script in crm 2013, it displays Login Page please tell me what did I mistake in my code?

    function getId()
    {
        var xml, xHReq;
        var resultXml, errorCount, msg, returnValue, returnId;

        var contact = Array();
         contact = Xrm.Page.getAttribute("new_predecessorid").getValue();
    alert(contact[0].id.toString());
       alert(contact[0].name);

        // Define the SOAP XML to access Microsoft Dynamics CRM Web service.
        xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soap:Envelope xmlns:soap=" +
            "\"http://schemas.xmlsoap.org/soap/envelope/\" " +
            "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
             "<soap:Body>" +
        // Specify the RetrieveMultiple message.
            "<RetrieveMultiple xmlns=" +
            "\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        // Specify that this is a QueryByAttribute query.
            "<query xmlns:q1=" +
            "\"http://schemas.microsoft.com/crm/2006/Query\" " +
            "xsi:type=\"q1:QueryByAttribute\">" +
        // Query the customeraddress entity.
            "<q1:EntityName>task</q1:EntityName>" +
        // Set the columns you want to return.
            "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
            "<q1:Attributes>" +
            "<q1:Attribute>scheduledend</q1:Attribute>" +

            "</q1:Attributes>" +
            "</q1:ColumnSet>" +
        // Specify the attribute that you are querying on.
            "<q1:Attributes>" +
            "<q1:Attribute>taskid</q1:Attribute>" +
            "</q1:Attributes>" +
        // Set the value of the attribute using the customerid
        // value of the case record.
            "<q1:Values>" +
            "<q1:Value xsi:type=\"xsd:string\">" +
            contact[0].id +
            "</q1:Value>" +
            "</q1:Values>" +
            "</query>" +
            "</RetrieveMultiple>" +
            "</soap:Body>" +
            "</soap:Envelope>";

        // Create an instance of an XMLHTTP object.
        xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        // Configure the XMLHttp object for the Microsoft CRM Web services.
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader(
            "SOAPAction",
            "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"
            );
        xmlHttpRequest.setRequestHeader(
            "Content-Type", "text/xml; charset=utf-8"
            );
        xmlHttpRequest.setRequestHeader(
            "Content-Length", xml.length
            );
        // Send the XMLHttp request.
        xmlHttpRequest.send(xml);
        // Capture the XMLHttp response in XML format.
        resultXml = xmlHttpRequest.responseXML;

        errorCount = resultXml.selectNodes('//error').length;
        // alert(errorCount.toString());
     if (errorCount != 0) {

            msg = resultXml.selectSingleNode('//description').nodeTypedValue;

            alert("An error has been occured on loading Customer Information\nError Details: " + msg);

        }

        else {

            if (resultXml.selectNodes("//q1:scheduledend") != null) {




                var req = resultXml.selectSingleNode("//q1:scheduledend").nodeTypedValue;
                Xrm.Page.getAttribute("scheduledend").setValue(req);
            }

            else {

                alert("CRM 2013 encountered an error while fetching for email address");

            }

        }
    }

    Friday, March 21, 2014 6:36 AM

All replies

  • Hi there,

    This function is no longer available because 2007 endpoint is not available.

    To get information you will have to use Organization.svc or OrganizationData.svc endpoints.

    Check following article - http://msdn.microsoft.com/en-us/library/gg490659.aspx

    Friday, March 21, 2014 9:11 AM
  • Hi tiagolvsantos,

    Can you modify my code ? Please add  OrganizationData.svc in my code ?

    Friday, March 21, 2014 9:20 AM
  • Hi,

    When i run this script the value of userRequest.status is 0. Please identify my mistake 

    function getCurrentUserFullName()
    {
        var serverUrl;
    var account=Xrm.Page.getAttribute("new_predecessorid").getValue();
    alert("1");
        if (Xrm.Page.context.getClientUrl !== undefined)
       {
    alert("2");
            serverUrl = Xrm.Page.context.getClientUrl();
        }
    else
        {
            serverUrl = Xrm.Page.context.getServerUrl();
        }
    alert("3");
        var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
        var userRequest = new XMLHttpRequest();
    alert(userRequest);    
    userRequest.open("GET", ODataPath + "/TaskSet(guid'" +account[0].id+ "')", true);
        userRequest.setRequestHeader("Accept", "application/json");
        userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        userRequest.send();
        if (userRequest.status === 200)
        {
            var retrievedUser = JSON.parse(userRequest.responseText).d;
            var userFullName = retrievedUser.ScheduledEnd;
            alert(userFullName);
        }
        else  
        {
            return "error";
        }
    }

    Friday, March 21, 2014 11:06 AM