Archive
Passing multiple values to web resource(HTML/Silverlight) in CRM 2011
Lets assume a requirement as below
- I need to show a custom HTML page on my “Account” form
- Read few “Account” entity field values (i.e., Address1_line1,City etc…) from my HTML page
- Account fields to pass to HTML need to be configurable (i.e., Fields I need to read in my HTML may change)
Solution
In CRM 2011, we can
- Add a HTML file as web resource and add it to entity form
- We can pass a single custom parameter called data
- Since only one value can be passed within the data , we can pass a multi-line string separated by special character
Implementation
- Create a blank HTML file using visual studio and add this as web resource to CRM
- Open the “Account” customization form and add the HTML web resource added in above step
- After you chosen the HTML as web resource, you get a “Web Resource Properties” section and looks as below
- In the “Web Resource Properties” section, below is the description of each field
- Custom Parameter(data) – The values you want to pass to your web resource.
- Since I want Address1_line1,City fields of account, I provided those 2 fields separated by ,(comma)
- We can read the value passed as query string
- Sample query string looks as ?data=%22emailaddress1%22%2c%22address1_city%22
- Restrict cross-frame scripting – Uncheck this to read content from CRM form
- Pass record object-type code and unique identifier as parameters – By choosing this, we can read entity information(i.e., Record Id, orglcid, orgname,objecttypecode etc..) as query string parameters
- The sample query string looks as id=%7b44623C6A-4452-E211-8FB3-00155DC87C64%7d&orglcid=1033&orgname=ABCD&type=1&typename=account&userlcid=1033
- Custom Parameter(data) – The values you want to pass to your web resource.
- Now open the “HTML” web resource and copy and paste the below content
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title></title>
<script type=”text/javascript”>
document.onreadystatechange = function () {
// If page load completes call the “getDataParam()” to read parameters
if (document.readyState == “complete”) {
getDataParam();
}
}
function getDataParam() {
var vals = new Array();
// The parameters we pass comes as query string
// Read it using “location.search”
if (location.search != “”) {
vals = location.search.substr(1).split(“&”);
for (var i in vals) {
vals[i] = vals[i].replace(/\+/g, ” “).split(“=”);
}
// Look for the parameter named ‘data’ among query string parameters
for (var i in vals) {
if (vals[i][0].toLowerCase() == “data”) {
// Call parseDataValue to read params passed in “Custom Parameter(data)”
parseDataValue(vals[i][1]);
break;
}
}
}
}
function parseDataValue(datavalue) {
if (datavalue != “”) {
var vals = new Array();
vals = decodeURIComponent(datavalue).split(“&”);
// You get “emailaddress1″,”address1_city” as a string in vals[0]
// Split with , (comma) to read the schema name
var email_schmaname = vals[0].split(‘,’)[0];
var city_schmaname = vals[0].split(‘,’)[1];
// Get the values of account’s email & city using the schema names we passed in (data)
var emailAddress = window.parent.Xrm.Page.data.entity.attributes.get(eval(email_schmaname)).getValue();
var city = window.parent.Xrm.Page.data.entity.attributes.get(eval(city_schmaname)).getValue();
alert(“Account’s email – ” + emailAddress);
alert(“Account’s city – ” + city);
}
}
</script>
</head>
<body>
</body></html>
The HTML file script has 2 functions
- getDataParam: Called from the body.onload event, this function retrieves any query string parameters passed to the page and locates one named data.
- parseDataValue: Receives the “data” parameter from getDataParam function.
Points to ponder
- We can’t access ‘Xrm’ object directly, use window.parent.Xrm
- document.readyState == “complete” will become true if the page load complets
- Refer the inline comments of HTML file to understand the jscript
🙂