2
votes

We are upgrading CRM 2011 to CRM 2013. The Custom Code Validation tool flagged the method: GenerateAuthenticationHeader().

The question is what would I replace this method to be so that it works in CRM 2013. As it is giving me "undefined" when I run it.

I have looked up many sources. One site says uses SDK REST, but it does not give the appropriate information.

If you can kindly let me know how I can replace the method GenerateAuthenticationHeader() it would be much appreciated.

The code is given below:

function GetAttributeValueFromID(sEntityName, sGUID, sAttributeName, isTextField)
{

var 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\">" + GenerateAuthenticationHeader() +
"  <soap:Body>" +
"    <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
"      <Request xsi:type=\"RetrieveRequest\" ReturnDynamicEntities=\"false\">" +
"        <Target xsi:type=\"TargetRetrieveDynamic\">" +
"          <EntityName>" + sEntityName + "</EntityName>" +
"          <EntityId>" + sGUID + "</EntityId>" +
"        </Target>" +
"        <ColumnSet xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:ColumnSet\">" +
"          <q1:Attributes>" +
"            <q1:Attribute>" + sAttributeName + "</q1:Attribute>" +
"          </q1:Attributes>" +
"        </ColumnSet>" +
"      </Request>" +
"    </Execute>" +
"  </soap:Body>" +
"</soap:Envelope>" + "";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");

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

xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

xmlHttpRequest.send(xml);

var result = null;

if (isTextField)
{
    result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName).text;

}
else
{

    result = xmlHttpRequest.responseXML.selectSingleNode("//q1:" + sAttributeName).getAttribute('name');

}
if (result == null)
{
    return '';
}
else
    return result;

}
1

1 Answers

3
votes

GenerateAuthenticationHeader() is deprecated in 2013 onwards. You have to rewrite your code using OData endpoint (FYI - this endpoint also deprecated in Dynamics 365 v9 after REST Web API)

There is no need to have this function anymore because 2007 endpoint is not available anymore. 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

Reference

Each of these web services can use the authentication provided by the Microsoft Dynamics 365 application within web resources without the need to include any code to implement authentication.

Update:

Download & import CRM REST Builder - this is a managed solution which can be imported & used within CRM to build/test queries.

Sample request URI is below, you can parameterize it.

var oDataURI = Xrm.Page.context.getClientUrl()
       + "/XRMServices/2011/OrganizationData.svc/"
       + "AccountSet(guid'" + accountId + "')"
       + "?$select="
       + "Address1_PostalCode";

Learn more