Accessing Other Web Services
Other Web services can be accessed using XMLHttp requests from JScript in form and field events or from ISV.Config buttons.
Note When a Web service is located outside the domain of the Microsoft Dynamics CRM Web application, Microsoft Dynamics CRM users must have Internet Explorer security settings set to enable access to data sources across domains. By default, this is enabled when users add the Microsoft Dynamics CRM Web site to their list of trusted sites. See http://www.microsoft.com/windows/ie/ie6/using/howto/security/setup.mspx for more information about security settings in Internet Explorer.
The following example accesses a public Web service, the Live Search Web service. This Web service allows you to add search capability to a Microsoft Dynamics CRM form.
Details about using the Live Search Web service is available at http://dev.live.com/livesearch/. An interactive SDK available at http://dev.live.com/livesearch/sdk/ allows you to learn about additional options to enhance the search.
Note Before you can use the Live Search Web service you must register to receive an Application ID. To create and manage Application IDs, see http://search.msn.com/developer.
This sample includes code added to the OnChange event of the Lead Company Name field. When the text in the field changes, the Jscript code for the event will access the Live Search Web Service and display the results within the contents of an IFrame you will add for that purpose.
This sample requires that an IFrame be added to the Lead entity form with the following property values set:
| Property | Value |
| Name | IFRAME_LiveSearch |
| URL | /_static/blank.htm |
For best results, the IFrame should be on the same tab as the Lead Company Name field.
Note For information about adding an IFrame to a Microsoft Dynamics CRM form, see the Microsoft Dynamics CRM Help topic Edit Entity Forms: Edit the main form for an entity.
The page /_static/blank.htm is an empty HTML page included in the Microsoft Dynamics CRM Web site. This page provides a valid target for an IFrame that may be manipulated by using Jscript code in form and field events. This sample will use the contentWindow property of the IFrame to change the underlying document.body.innerHTML so that the search results are displayed.
Apply the following code to the Lead Company Name Field OnChange event.
Note For information about adding script to an OnChange event for a field, see the Help topic Work with Entities: Edit the main form for an entity. See the instructions within the section titled Add or edit a field.
// Provide your own Live Search Application ID.
// For more information,
// see http://search.msn.com/developer.
var appId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// Get the text of the Lead Company Name field.
var searchString = crmForm.all.companyname.DataValue;
// Set a reference to the IFRAME.
var oIFrame = crmForm.all.IFRAME_LiveSearch;
// Get the first set of results.
var offset = 0;
// Return only five results.
var count = 5;
// Set the culture to English - United States.
var cultureInfo = "en-us";
// Instantiate an XMLHTTP object.
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// Define the request using SOAP.
var query = "<?xml version='1.0' encoding='UTF-8'?>";
query += "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://";
query += "schemas.xmlsoap.org/soap/envelope/'";
query += " xmlns:xsi=";
query += "'http://www.w3.org/1999/XMLSchema-instance'";
query += " xmlns:xsd=";
query += "'http://www.w3.org/1999/XMLSchema'> ";
query += "<SOAP-ENV:Body><ns1:Search xmlns:ns1='";
query += "http://schemas.microsoft.com/";
query += "MSNSearch/2005/09/fex'>";
query += "<Request><AppID xsi:type='xsd:string'>"
query += appId + "</AppID>";
query += "<Query xsi:type='xsd:string'>";
query += searchString +"</Query>";
query += "<CultureInfo xsi:type='xsd:string'>";
query += cultureInfo +"</CultureInfo>";
query += "<SafeSearch ";
query += "xsi:type='xsd:string'>Moderate</SafeSearch>";
query += "<Requests><SourceRequest>";
query += "<Source xsi:type='xsd:string'>Web</Source>";
query += "<Offset xsi:type='xsd:int'>"+offset+"</Offset>";
query += "<Count xsi:type='xsd:int'>"+count+"</Count>";
query += "<ResultFields ";
query += "xsi:type='xsd:string'>All</ResultFields>";
query += "</SourceRequest></Requests>";
query += "</Request></ns1:Search>";
query += "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
// Open the Live search Web service.
// Catch the expected error if the user does not have
// Internet Explorer security settings set to allow
// access to data sources across domains.
try
{
var uri = "http://soap.search.msn.com/webservices.asmx";
xmlhttp.open("get", uri, false);
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.setRequestHeader("SOAPAction", "Search");
// Send the query.
xmlhttp.send(query);
// Get the responses.
var rx = xmlhttp.ResponseXML;
var sr = rx.getElementsByTagName("SourceResponse");
}
catch (e)
{
var msg = "<p>There was a problem with the Search</p>";
if (e.number == -2146828218)
{
msg += "<p>Your Internet Explorer security settings ";
msg += "for this zone do not allow ";
msg += "access to data sources across domains. </p>";
msg += "<p>You must set your Internet Explorer ";
msg += "security settings to allow access ";
msg += "to data sources across domains. ";
msg += "For more information see ";
msg += "<a href='http://www.microsoft.com/windows/ie/";
msg += "ie6/using/howto/security/setup.mspx#EQCAC' ";
msg += "Target='_blank' >Setting Up Security Zones";
msg += "</a>.</p>";
}
// Return the error message to the IFrame.
oIFrame.contentWindow.document.body.innerHTML = msg;
return;
}
var total;
// Try to get the total number of responses or
// find out if there was a problem.
try
{
total = sr.item(0).getElementsByTagName("Total");
}
catch (e)
{
var msg = "<p>There was a problem with the Search</p>";
if (e.number == -2146823281)
{
msg += "<p>The Web service code may not be using a ";
msg += "valid Live Search Application ID.</p>";
}
// Return the error message to the IFrame.
oIFrame.contentWindow.document.body.innerHTML = msg;
return;
}
// Find out if there were no results returned.
if (total.item(0).text == "0")
{
var msg;
msg = "<p>Your search returned no results.</p>";
// Return the message to the IFrame.
oIFrame.contentWindow.document.body.innerHTML = msg;
return;
}
// Start building the HTML to display results in the
// IFrame.
var display = "<html><head />";
display += "<body>";
display += "<ul style='font-family:Tahoma;margin-bottom:0px;";
display += "margin-top:0px;margin-left:0px;'>";
// Get the results from the Search.
var results = sr.item(0).getElementsByTagName("Result");
var n_results = results.length;
//Loop through the results generating List Item elements.
for (i=0; i< n_results; i++)
{
var Result = results.item(i);
var Title = Result.selectSingleNode("Title").text;
var resultUrl = encodeURI(Result.selectSingleNode("Url").text);
// Sometimes there is no description.
var Desc = "";
try
{
Desc = Result.selectSingleNode("Description").text;
}
catch(e)
{
Desc = "No Description Available";
}
display += "<li style='margin-bottom:3px;font-size:14px;";
display += "border-bottom:1px solid black;padding-bottom:3px;'>";
display += "<a target='_blank' href='"+resultUrl+"'>"
display += Title+"</a>";
display += "<span style='font-size:12px;'> : "+Desc+"</span>";
display += "<div style='font-size:10px;'nowrap>";
display += "<a target='_blank' href=";
display += resultUrl+ ">";
display += resultUrl+"</a></div>";
display += "</li>";
}
display += "</ul>";
display += "<a style='font-family:Tahoma;font-size:10px;' ";
display += "target='_blank' href=";
display += "'http://search.live.com/results.aspx?q=";
display += searchString+"&mkt="+cultureInfo+"'>";
display += "Get more results for '"+searchString+"'.";
display += "</a></body></html>";
// Remove the margin at the top of the IFrame.
oIFrame.contentWindow.document.body.style.marginTop= '0px';
//Set the contents of the IFrame using the HTML that is generated.
oIFrame.contentWindow.document.body.innerHTML = display;
© 2009 Microsoft Corporation. All rights reserved.