Technical CRM 2011: Silverlight access to fields on a form

Technical CRM 2011: Silverlight access to fields on a form

A common way to extend CRM 2011 is by adding a custom Silverlight (SL) controls on web pages. There are lots of samples in the CRM 2011 SDK for how to get your custom SL running in CRM.

But one common request I have seen that wasn't that straight forward was how to access fields on the CRM form that you launched your SL from.

There are 2 techniques that when used together make this a simple process:

  1. Silverlight's built in helper class HtmlPage.Window.Eval
    1. This allows you to 'inject' JavaScript and run it on the page that is hosting your SL control.
  2. CRM's Client side SDK
    Xrm.Page
    1. This allows you to interact with a CRM form via JavaScript

Below is a sample Silverlight method that takes in a field name and returns
the
String
value of Lookup on the CRM Form.

private
String GetLookupAsString(String lookupName)

{

String field = System.Windows.Browser.HtmlPage.Window.Eval("window.opener.Xrm.Page.getAttribute('"

+ lookupName + "').getValue()[0].name").ToString();

 

return field;

}

Using this pattern you should be able to interact with CRM forms from Silver Light to your heart's content!

More places to learn about Silver Light with CRM 2011:


Microsoft Dynamics CRM Blog