Advertisements

Archive

Posts Tagged ‘web resource’

Different ways of referencing web resources – CRM

December 8, 2014 Leave a comment

If you have a HTML file added as a Web resource in CRM and it need to refer below web resources

  • new_jquery
  • new_dummy

Below are 2 ways to achieve the same

Standard Way

One way is to follow below convention to refer the web resource files in your HTML file

<script src=”../webresources/{webresourcename}”></script>

Referencing by folders

Assume you maintain files in folder structure, in your Visual studio solution which looks as below

  • Jscript
    • Util
      • jquery.js
      • helper.js
    • Account
      • account_form.js
      • account_ribbon.js
  • CSS
    • dummy.css

Below is a more readable way to refer CRM web resources in HTML file.

Below are steps to achieve same.

Step 1

Add web resources with name as “solution prefix_/{webresourcename}”, as below.

  • new_/Jscript/Util/jquery
  • new_/CSS/dummy
Web resource with folder naming

Web resource with folder naming

Step 2 

When you follow above naming convention, web resources can be referenced like how we do it in Asp.net projects, as below

<script src=”Jscript/Util/jquery” type=”text/javascript”></script>

Refer MSDN Link

Advertisements
Categories: CRM Tags: , ,

Passing multiple values to web resource(HTML/Silverlight) in CRM 2011

March 7, 2013 2 comments

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
Custom Parameter (data)

Custom Parameter (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
Add web resource to Account entity

Add web resource to Account entity

  • After you chosen the HTML as web resource, you get a “Web Resource Properties” section and looks as below
Web Resource Properties

Web Resource Properties

  • 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
  • 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”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;

<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

🙂