Saturday, 26 May 2012

Open Silverlight application on click of custom ribbon button in CRM 2011

In this post we will see how we can open a Silverlight application on click of a custom button in CRM 2011.

First of all create a Silverlight web application which you want to execute on button click in CRM application.

Compile your Silverlight Application. In the web project of your application you will find a ClientBin folder. When you compile your application a xap file is created under the ClientBin folder. Create a webresource of type xap and upload the xap file created under ClientBin folder.

Now second step is to embed the Silverlight application in an html file. Create an html file within your Silverlight application web project and add this code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
    <style type="text/css">
    html, body {
        height: 100%;
        overflow: auto;
    body {
        padding: 0;
        margin: 0;
    #silverlightControlHost {
        height: 100%;
    <script type="text/jscript" src="../../ClientGlobalContext.js.aspx"></script>
   2:     <script type="text/javascript" src="../JavaScripts/Silverlight.js">
   1: </script>
   2:     <script type="text/javascript">
   3:       function onSilverlightError(sender, args) {
   5:             var appSource = "";
   6:             if (sender != null && sender != 0) {
   7:               appSource = sender.getHost().Source;
   8:             }
  10:             var errorType = args.ErrorType;
  11:             var iErrorCode = args.ErrorCode;
  13:             if (errorType == "ImageError" || errorType == "MediaError") {
  14:               return;
  15:             }
  17:             var errMsg = "Unhandled Error in Silverlight Application " +  appSource + "\n" ;
  19:             errMsg += "Code: "+ iErrorCode + "    \n";
  20:             errMsg += "Category: " + errorType + "       \n";
  21:             errMsg += "Message: " + args.ErrorMessage + "     \n";
  23:             if (errorType == "ParserError") {
  24:                 errMsg += "File: " + args.xamlFile + "     \n";
  25:                 errMsg += "Line: " + args.lineNumber + "     \n";
  26:                 errMsg += "Position: " + args.charPosition + "     \n";
  27:             }
  28:             else if (errorType == "RuntimeError") {           
  29:                 if (args.lineNumber != 0) {
  30:                     errMsg += "Line: " + args.lineNumber + "     \n";
  31:                     errMsg += "Position: " +  args.charPosition + "     \n";
  32:                 }
  33:                 errMsg += "MethodName: " + args.methodName + "     \n";
  34:             }
  36:             throw new Error(errMsg);
  37:         }
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
      <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
          <param name="source" value="../XAP/ExpenseNote.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="4.0.50826.0" />
          <param name="autoUpgrade" value="true" />
          <a href="" style="text-decoration:none">
               <img src="" alt="Get Microsoft Silverlight" style="border-style:none"/>
        </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>


1) In the source parameter specify the path of your xap webresource correctly.
Here name of the webresource is ink_/XAP/ExpenseNote.xap and so the value for source is “../XAP/ExpenseNote.xap”, a relative path.

2) Also add the SilverLight.js file as a webresource. You can find the Silverlight.js file in sdk in the Silverlight example projects.

Now create a webresource and upload this html file. We have named this html webresource as CreateExpenseNote.html.

Now we need to open this html file on click of a button.

For this we will create a button. You can create button using the Visual Ribbon editor. To use Visual Ribbon Editor refer this link

In the Visual Ribbon Editor define the action for the button as given below:


In Action you can either directly specify a URL which will be accessed on button click or you can specify a javascript function which is defined within a webresource of type javascript file. In this example we would be using a javascript function defined within a webresource.

Here OpenSilverLightControl is the function that contains code to open the CreateExpenseNote.html file and OpenExpenseNote.js is the webresource within which the function is defined.

function OpenSilverLightControl()
var serverUrl = Xrm.Page.context.getServerUrl();          window.showModalDialog(serverUrl+'/WebResources/ink_/html/CreateExpenseNote.html', " ","dialogWidth:800px ; dialogHeight:600px; center:yes;resizable:yes");

So on button click the CreateExpenseNote.html file which embeds the Silverlight application, is displayed as a ModalDialog .

Friday, 4 May 2012

Dynamics CRM 2011 - Client side Context Reference

Today, I came across couple of very useful links on Client-Side Context Reference.

I would like to post the consolidated details of the same here in below post.

Your code will need to operate within a context where a number of variables are not known at design time. The context object provides methods which can be used to retrieve information that is specific to a user, an organization or parameters passed to a page.

Following are the methods of the context object:-

· getAuthenticationHeader
It is a deprecated method used to return the encoded SOAP header which is necessary to use Dynamics CRM 4.0 web service calls using Microsoft Jscript.

· getCurrentTheme
This method returns a string which represents the theme currently chosen by the user in Microsoft Office Outlook.

· getOrgLcid
This method returns the LCID value representing the base language for the organization.

· getOrgUniqueName
It returns the unique text value of the organizations name.

· getQueryStringParameters
This function returns the query string arguments as an array of key value pairs.

· getServerUrl
This method returns the base server URL. The URL format varies for on-premises Microsoft Dynamics CRM 2011, Microsoft Dynamics CRM Online or working offline with Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access.

· getUserId
It returns GUID value of the SystemUser.Id value for the current user.

· getUserLcid
This method returns the LCID value of the Language Pack that the user selected as their preferred language.

· getQueryStringParameters
This function returns the query string arguments as an array of key value pairs.

 · getUserRoles
This function returns an array of string that represent the GUID values of each of the security roles that the user is associated with. This method does not return GUID values that represent the team security roles that the user may belong to.

 · isOutlookClient
This function returns a Boolean value which indicates if the user is using the Microsoft Dynamics CRM for Microsoft Office Outlook client.

 · isOutlookOnline
This function returns a Boolean value indicating whether the user is connected to the Microsoft Dynamics CRM server while using the Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access client. When this function returns false, the user is working offline without a connection to the server. They are interacting with an instance of Microsoft Dynamics CRM running on their local computer.

 · prependOrgName
It prepends the organization name to the specified path.

Drag and Drop Listbox

Glad to announce that “Drag and Drop Listbox” for Dynamics CRM 2011 was successfully placed at Dynamics Marketplace.

Give it a whirl.