How to develop yahoo widgets for accessing MSCRM 4.0 data: Part 2




This blog is in continuation to my last blog How to develop yahoo widgets for accessing MSCRM 4.0 data in which I had taken you through the development of yahoo widgets for accessing MSCRM 4.0 data for the on-premise deployment of MSCRM 4.0.
Now, I’ll explain how you can access MSCRM 4.0 data in the internet facing deployment (IFD) model of MSCRM 4.0.
Well, the first and foremost difference between the On-premise & IFD models is the strategy used for authentication, while fetching data from CRM using the web services .
In order to authenticate the web service calls in the hosted (IFD) model we need to specify the valid active directory user credentials to request for the issue of Crm Ticket. This Crm ticket will inherently be used in the crm authentication token provided to the soap request for authenticating the web service calls to fetch data from MSCRM.


See the JavaScript below to retrieve the crm ticket. Fire a soap request including valid user credentials and on authentication, MSCRM will in turn issue a crm ticket, which must be used further in web service calls to fetch the data.

function RetrieveCrmIFDTicket()
{



// Prepare the soap request
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">" +
" <soap:Body>" +
" <Execute xmlns="http://schemas.microsoft.com/crm/2007/CrmDiscoveryService">" +
" <Request xsi:type="RetrieveCrmTicketRequest">" +
" <OrganizationName>"+"MicrosoftCRM"+"</OrganizationName>" +
" <UserId><![CDATA["+strUserName+"]]></UserId>" +
" <Password><![CDATA["+strPassword+"]]></Password>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
//Note: strUserName & strPassword are the preference variables containing user credentials.


xmlHttpRequest = new XMLHttpRequest();

//Event to monitor the status of the request, asynchronously
xmlHttpRequest.onreadystatechange = CrmIFDTicketStatusProc;

xmlHttpRequest.open("POST", "localhost:5555 "+"/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx", true);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/CrmDiscoveryService/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

}

//Note: Replace the “MicrosoftCRM” with your organization name.



// Monitor the status of the request
function CrmIFDTicketStatusProc()
{

if ( this.readyState == 4 ) // request complete
{
if ( this.status == 200 ) // Success
{
var resultXml = this.responseXML;
var orgDetailNodes = resultXml.evaluate("soap:Envelope/soap:Body/ExecuteResponse/Response/OrganizationDetail");

//Extract the single Business entity node
var singleOrgDetailNode = orgDetailNodes.item(0);
var org = singleOrgDetailNode.evaluate("string(OrganizationName)");

//Replace the “MicrosoftCRM” with your organization name
if ( org == "MicrosoftCRM" )
{
crmServiceUrl = singleOrgDetailNode.evaluate("string(CrmServiceUrl)");
}
//Get the crm ticket
crmTicket = resultXml.evaluate("string(soap:Envelope/soap:Body/ExecuteResponse/Response/CrmTicket)");
}
}

}



//Retrieve the MSCRM userid for the currently logged in user
function WhoAmIRequest()
{

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">" +
" <soap:Header>" +
" <CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">" +
" <AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">2</AuthenticationType>" +
" <CrmTicket xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">"+crmTicket+"</CrmTicket>" +
" <OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">"+"MicrosoftCRM"+"</OrganizationName>" +
" <CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>" +
" </CrmAuthenticationToken>" +
" </soap:Header>" +
" <soap:Body>" +
" <Execute xmlns="http://schemas.microsoft.com/crm/2007/WebServices">" +
" <Request xsi:type="WhoAmIRequest" />" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";


xmlHttpRequest = new XMLHttpRequest();

// Event to monitor the status of the request, asynchronously
xmlHttpRequest.onreadystatechange = WhoAmIStatusProc;
xmlHttpRequest.open("POST", crmServiceUrl, true);
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);

// Send the request
xmlHttpRequest.send(xml);

}


// Monitor the status of the request
function WhoAmIStatusProc()
{
if ( this.readyState == 4 ) // request complete
{
if ( this.status == 200 ) // Success
{
var resultXml = this.responseXML;

// retrieve userId
UserId = resultXml.evaluate("string(soap:Envelope/soap:Body/ExecuteResponse/Response[@xsi:type='WhoAmIResponse']/UserId)");

if(UserId==null || UserId=="" )
{
throw "UserId is Null";
}
// Retreive the activities for the UserId Passed
IFDActivitiesRequest(UserId);
}

}


}



// Now retreive all activities for the user
function IFDActivitiesRequest(UserId)
{

// Prepare the soap request
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">" +
" <soap:Header>" +
" <CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">" +
" <AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">2</AuthenticationType>" +
" <CrmTicket xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">"+crmTicket+"</CrmTicket>" +
" <OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">"+"MicrosoftCRM"+"</OrganizationName>" +
" <CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>" +
" </CrmAuthenticationToken>" +
" </soap:Header>" +
" <soap:Body>" +
" <RetrieveMultiple xmlns="http://schemas.microsoft.com/crm/2007/WebServices">" +
" <query xmlns:q1="http://schemas.microsoft.com/crm/2006/Query" xsi:type="q1:QueryExpression">" +
" <q1:EntityName>activitypointer</q1:EntityName>" +
" <q1:ColumnSet xsi:type="q1:ColumnSet">" +
" <q1:Attributes>" +
" <q1:Attribute>activityid</q1:Attribute>" +
" <q1:Attribute>activitytypecode</q1:Attribute>" +
" <q1:Attribute>prioritycode</q1:Attribute>" +
" <q1:Attribute>scheduledend</q1:Attribute>" +
" <q1:Attribute>subject</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:PageInfo>" +
" <q1:PageNumber>" + "1"+ "</q1:PageNumber>" +
" <q1:Count>"+"10"+"</q1:Count>" +
" </q1:PageInfo>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>ownerid</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type="xsd:string">"+ UserId + "</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";


xmlHttpRequest = new XMLHttpRequest();

// Event to monitor the status of the request, asynchronously
xmlHttpRequest.onreadystatechange = IFDActivitiesRequestStatusProc;
xmlHttpRequest.open("POST", crmServiceUrl, true);
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 soap request
xmlHttpRequest.send(xml);

}



// Monitor the status of the request
function IFDActivitiesRequestStatusProc()
{
if ( this.readyState == 4 ) // completed
{
if ( this.status == 200 ) // success
{
//This is the final XML data retreived as a result of the webservice call
var resultXml = this.responseXML;
}
}
}




Comments

Popular posts from this blog

The key specified to compute a hash value is expired, only active keys are valid.

IFD Implementation for custom web pages and Anonymous Access

Using CRM Services to create or update records in large numbers