Freigeben über


Accessing CRM forms from Silverlight Web Resources

CRM 2011 allows developers to natively use Silverlight applications to extend the user experience. One of the main use cases for Silverlight apps is to embed them into CRM forms to provide extended controls or visualizations. While natural for experienced Silverlight developers, CRM developers might get a bit puzzled at first on how to talk back and forth between Silverlight and CRM forms

The key is to use Silverlight’s HTML bridge. At the end of the day the form APIs are nothing more than JScript interfaces. All you have to do in your Silverlight app is to use the HTML bridge to access/invoke the method or property that you need. Here is a simple example, this code will allow you to retrieve the value of a text control in a CRM form from a Silverlight application

This is just a generic method that does a plain JScript “eval” to call the corresponding CRM API. I know I know, eval could be evil but I’m just lazy and like to keep a similar syntax whether I’m invoking a command from Silverlight or from pure JScript.

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Browser;

...

namespace MyApp

{

    public class CrmUtils

    {

public static string executeXrmCommand(string command, bool throwIfInvalid)

        {

            //same as getting a property, just that the return value will be null

 

       Object result = HtmlPage.Window.Eval("try {"+ command + "} catch(e){ };");

                if (result != null)

                {

                    return result.ToString();

                }

                else if (throwIfInvalid)

                {

                    throw new ArgumentException("Invalid Xrm Command");

  }

                else

                {

                    return null;

                }

    

        }

}

}

 

 

string recordId = CrmUtils.executeXrmCommand("Xrm.Page.data.entity.getId()", true);

 

The CRM’s client side API syntax (Xrm.Page…) is documented in the CRM SDK

Happy coding

Then, when you actually need to access a control or invoke a function you would do something like:

Comments

  • Anonymous
    February 23, 2011
    Here's a puzzled, thankful CRM developer. Thank you!