serving the solutions day and night

Pages

Saturday, January 12, 2013

MS Dynamics CRM Web Resources

Web resources are virtual files that are stored in the CRM database and that you can retrieve by using a unique URL address. Solution -> Entities -> Web Resource

Limitations of Web Resources
Web resource does not supports ASP.NET(.aspx or asmx) page to execute code on the server. Web resources are either static files or files with code that is processed client-side by the browser.

Web resources are only available by using the CRM web application security context. Only licensed CRM users who have the necessary privileges can access them.

Size Limitations - The maximum size of files that can be uploaded is determined by the Organization.MaxUploadFileSize property.
Settings -> System -> Administration -> System Settings -> E-mail tab -> Maximum file size (default 5 mb). This setting limits the size of files that can be attached to email messages, notes, and web resources.
Web Resource Properties - Name, Display Name, Description, Type, Language


Web Resource Types

FileFile extensionsType
Webpage (HTML).htm, .html1
Style Sheet (CSS).css2
Script (JScript).js3
Data (XML).xml4
Image (PNG).png5
Image (JPG).jpg6
Image (GIF).gif7
Silverlight (XAP).xap8
StyleSheet (XSL).xsl, .xslt9
Image (ICO).ico10


CRM also displays a text editor option, in which you can create or edit a web resource.

CRM generates a URL for the web resource, which is used to reference the web resource in custom applications. Clicking the link in the URL field will launch the web resource in a browser.

For example, Name -> [gab_] /html/test.html
URL - > http://<CRM_URL>/WebResources/gab_/html/test.html

Web Resources -> Managed Properties - to customize the web resource.

Referencing Web Resources

$webresource Directive -  when referencing a web resource from a ribbon control or from a SiteMap sub area.
    $webresource:<name of Web Resource>
    $webresource:gab_/html/test.html


To display a Silverlight web resource outside an entity form or chart, create an HTML web resource to be the host page for the Silverlight web resource. Then use the $webresource: directive to open the HTML web resource.

Xrm.Utility.openWebResource function will open an HTML web resource in a new window with parameters to pass the name of the web resource, any query string data to be passed in the data parameter, and information about height and width of the window.

Web Page (HTML) - A Web page (HTML) or Silverlight Web resource page can only accept a single custom query string parameter called data. To pass more than one value within the data parameter, you need to encode the parameters and decode the parameters within your page.

For example, the parameters you want to pass are: first=First Value&second=Second Value&third=Third Value
The encoded values should be: first%3DFirst%20Value%26second%3DSecond%20Value%26third%3DThird%20Value

URL : http://<CRM_URL>/WebResources/gab_/test.html?Data=first%3DFirst%20Value%26second%3DSecond%20Value%26third%3DThird%20Value

Passing Parameters to HTML Web Resources
ParameterNameDescription
typenameEntity NameThe name of the entity.
typeEntity Type CodeAn integer that uniquely identifies the entity in a specific organization.
idObject GUIDThe GUID that represents a record.
orgnameOrganization NameThe unique name of the organization.
userlcidUser Language CodeThe language code identifier being used by the current user.
orglcidOrganization Language CodeThe language code identifier that represents the base language for the organization.
dataOptional Data ParameterAn optional value that may be passed.
formidForm IdThe GUID that represents a form id.
pagemodeFor internal use only.
securityFor internal use only.
tabSetFor internal use only.


1)Create a Web page (HTML) web resource (Solution -> Entities -> Web Resource (HTML)) (gab_/html/test.htm)
2)Add a Web Page web resource to a form.
  Expand Entities -> Select entity -> Select Main Form -> Click Header - > Click Insert tab -> Click Web Resource.


Script (JScript)
1)Create a Style Sheet (CSS) web resource (Solution -> Entities -> Web Resource (Script) (gab_/script/test.js)
function BP() {
    var ln = Xrm.Page.data.entity.attributes.get("gab_firstname").getValue();
    var fn = Xrm.Page.data.entity.attributes.get("gab_lastname").getValue();
    var c = Xrm.Page.data.entity.attributes.get("gab_city").getValue();
    var t = checkEmpty(c)  +  checkEmpty(fn)  + checkEmpty(ln);
    Xrm.Page.ui.controls.get("gab_title").setDisabled(true);
    Xrm.Page.data.entity.attributes.get("gab_title").setValue(t);
    alert(t);
}

function checkEmpty(value) {
    return (value == null) ? " " : " " + value;
}
2)Reference Script on the form
3)Expand Entities -> Select entity -> Select Main Form -> Click Form Properties -> Add Form Libraries (gab_script/test.js)

4)Double click the Last Name Field -> Click Events tab -> Under the Event Handlers Section, Click Add -> Select Library(gab_script/test.js) -> Function "BP" -> Click OK button -> Click Save and  Publish.


Style Sheet (CSS)
1)Create a Style Sheet (CSS) web resource (Solution -> Entities -> Web Resource) (gab_/css/test.css)
2)Add CSS to (test.htm)
    <link rel="stylesheet" type="text/css" href="../css/test.css" />
3)Add a Web Page web resource to a form.

Data (XML) & Stylesheet (XSL)
gab_\html\xml.html
<!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 src="../script/xml.js" type="text/javascript"></script>
 <link href="../css/xml.css" rel="stylesheet" type="text/css" />
</head>
<body onload="SDK.ImportWebResources.showData()">
 <div id="results" />
</body>
</html>

gab_xslt\xml.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl"
>
 <xsl:output method="xml"
             indent="yes"/>

 <xsl:template match="@* | node()">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
 </xsl:template>


 <xsl:template match="people">
  <xsl:element name="table">
   <xsl:element name="thead">
    <xsl:element name="tr">
     <xsl:element name="th">
      <xsl:text>First Name</xsl:text>
     </xsl:element>
     <xsl:element name="th">
      <xsl:text>Last Name</xsl:text>
     </xsl:element>
    </xsl:element>
   </xsl:element>
   <xsl:element name="tbody">
    <xsl:apply-templates />
   </xsl:element>
  </xsl:element>

 </xsl:template>

 <xsl:template match="person">
  <xsl:element name="tr">
   <xsl:element name="td">
    <xsl:value-of select="@firstName"/>
   </xsl:element>
   <xsl:element name="td">
    <xsl:value-of select="@lastName"/>
   </xsl:element>
  </xsl:element>

 </xsl:template>

</xsl:stylesheet>

gab_\xml\xml.xml
<?xml version="1.0" encoding="utf-8" ?>
<people>
 <person firstName="Apurva"
         lastName="Dalia" />
 <person firstName="Ofer"
         lastName="Daliot" />
 <person firstName="Jim"
         lastName="Daly" />
 <person firstName="Ryan"
         lastName="Danner" />
 <person firstName="Mike"
         lastName="Danseglio" />
 <person firstName="Alex"
         lastName="Darrow" />
</people>

gab_\script\xml.js
//If the SDK namespace object is not defined, create it.
if (typeof (SDK) == "undefined")
{ SDK = {}; }
// Create Namespace container for functions in this library;
SDK.ImportWebResources = {
 dataFile: "../xml/xml.xml",
 transformFile: "../xslt/xml.xslt",
 showData: function () {

  //Create an XML document from the Data.xml file
  var dataXml = new ActiveXObject("Msxml2.DOMDocument.6.0");
  dataXml.async = false;
  dataXml.load(this.dataFile);

  //Create an XML document from the Transform.xslt file
  var transformXSLT = new ActiveXObject("Msxml2.DOMDocument.6.0");
  transformXSLT.async = false;
  transformXSLT.load(this.transformFile);

  // Set the innerHTML of the results area to the output of the transformation.
  var resultsArea = document.getElementById("results");
  resultsArea.innerHTML = dataXml.transformNode(transformXSLT);
 }
}

gab_\css\xml.css
body
{
    font-family: Calibri;
}
table
{
    border: 1px solid gray;
    border-collapse: collapse;
}
th
{
    text-align: left;
    border: 1px solid gray;
}
td
{
    border: 1px solid gray;
}



http://msdn.microsoft.com/en-us/library/gg309473.aspx

No comments: