Geeks With Blogs
Nilesh Baviskar
Here is the screenshot of final output.


Here is what PoC is doing
1.       There is Silverlight application hosted in CRM on custom area-subarea. This in-turn needs two web resources
2.       There is Custom button added in Ribbon
3.       Third web resource is hosting JavaScript function
4.       On click of Custom Button on CRM Ribbon function in JavaScript web resource is called which in-turn calls the method in C# code of Silverlight application. String input is passed to this method
5.       C# method is converting the input string to upper case and returning it.
6.       Finally alert is displayed with upper case string.
Here are the details to create the PoC
1.       Created new solution in CRM
2.       Created new Area and Sub-Area for this PoC by editing Site Map XML. Here is the snapshot of XML changes.
            Title="Custom Area For RibbonPoC"
                   Title="Custom Sub-Area For RibbonPoC"
                   Url="$webresource:new_/RibbonPocTestPage.html" />
3.       Added custom button in Application Ribbon. Here is the updated XML for Ribbon
                  Image32by32="/_imgs/resourcecenter_24x24.gif" />
        <EnableRules />
        <DisplayRules />
                              Library="$webresource:new_RibbonTestJavaScript" />
      <TabDisplayRules />
      <DisplayRules />
      <EnableRules />
          <Titlelanguagecode="1033"description="Custom Button." />
          <Titlelanguagecode="1033"description="Custom Button" />
4.       Created Silverlight Application. You can look at my Article Hosting Silverlight 4.0 application in Dynamics CRM 2011
Here is important C# code. Note System.Windows.Browser.HtmlPage.RegisterScriptableObject("SilverlightCode", this); and [ScriptableMember]
public partial class MainPage : UserControl
        public MainPage()
            System.Windows.Browser.HtmlPage.RegisterScriptableObject("SilverlightCode", this);                               
        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
            foreach (UIElement child in LinksStackPanel.Children)
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
        private void button1_Click(object sender, RoutedEventArgs e)
            MessageBox.Show(CustomMethod("Silverlight Button Clicked !"));
        //This method will be called from JavaScript on click of Ribbon Button
        //This method needs to be Public
        public string CustomMethod(string message = "")
            //MessageBox.Show(message, "Message", MessageBoxButton.OK);
            return message.ToUpper();
Here is important HTML code. Note the <object id="SLFromJS"…
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
        <object id="SLFromJS" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
 <param name="source" value="ClientBin/RibbonPoC.xap"/>
 <param name="onError" value="onSilverlightError" />
 <param name="background" value="white" />
 <param name="minRuntimeVersion" value="4.0.50401.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>
5.       Hosted Silverlight Application in CRM. For this we need to create two web resources – one to host HTML and second for XAP.
6.       Created one more web resource to host JavaScript function. Developer tools (F12) in IE 8 helped me to find exact location of my Silverlight Object (SLFromJS) in HTML DOM. Here is the JavaScript – Note window.frames['contentIFrame'].document.forms['form1'].SLFromJS;
function CallSilverlightMethod(sender) {
    alert('Inside JS1!');
    var slc = window.frames['contentIFrame'].document.forms['form1'].SLFromJS;
    alert('Inside JS2!');
    if (slc != null) {
        alert('Inside if!');
        alert(slc.Content.SilverlightCode.CustomMethod('Msg From JavaScript'));
        alert('Going out of if!');
    alert('Out of if!');
My CRM solution looks like following now
7.       Done! Now test the work by opening link of HTML web resource.
Thanks to following blog posts which I referred.


Posted on Thursday, August 25, 2011 3:17 AM | Back to top

Comments on this post: Call C# code inside Silverlight application on click of Custom Ribbon Button - Dynamics CRM 2011

# re: Call C# code inside Silverlight application on click of Custom Ribbon Button - Dynamics CRM 2011
Requesting Gravatar...
I need help..
I did all the steps meansion here, but still I am getting error message when I load page.
Left by Hiren Gandi on Apr 15, 2012 7:30 AM

# re: Call C# code inside Silverlight application on click of Custom Ribbon Button - Dynamics CRM 2011
Requesting Gravatar...
Hi Hiren,

What is the error you are getting?

Left by Nilesh on Apr 15, 2012 8:12 AM

# re: Call C# code inside Silverlight application on click of Custom Ribbon Button - Dynamics CRM 2011
Requesting Gravatar...
I follow all the steps you meansion in this artical, but pages in not loading. When I try from VS 2010 my silverlight solution running fine, but when I try from CRM 2011 I am able to see entry to Sitmap and sub group but the right side frame is not show my silverlight page.

Thank you for your help.
Left by Hiren Gandhi on Apr 15, 2012 11:53 AM

# re: Call C# code inside Silverlight application on click of Custom Ribbon Button - Dynamics CRM 2011
Requesting Gravatar...
Hiren - Check my post of - Hosting Silverlight 4.0 application in Dynamics CRM 2011 at I guess your problem is in hosting the Silverlight Application and not Javascript.
Left by Nilesh on Apr 16, 2012 7:46 AM

Your comment:
 (will show your gravatar)

Copyright © Nilesh | Powered by: