Monday, 5 March 2012

Pass Data from an Entity form to an embedded Silverlight Web Resource

While developing a Silverlight Web Resource (XAP) to be viewed on an entity form in Dynamics CRM 2011, sometimes we require to access contextual information of the form. In this post I’ve explained the same with source code.
Below screen shot helps you in inserting a Silverlight web resource on your custom entity via entity customization. Go to default solution/your custom solution, open an entity form and click on Insert tab to insert a Web Resource.


To retrieve Object type code and the other contextual data you have to check the Pass record object-type code and unique identifier as parameters option.
Use custom parameter (data) to pass any data which you would like to use in your Silverlight user control highlighted in below screen shot.


These values can be accessible in Silverlight control via InitParams, a dictionary of key/value pairs. Below table will help you to use an appropriate parameter in accessing the form values.

Parameter Name Description
typename Entity Name The name of the entity.
type Entity Type Code An integer that uniquely identifies the entity in a specific organization.
id Object GUID The GUID that represents a record.
orgname Organization Name The unique name of the organization.
userlcid User Language Code The language code identifier being used by the current user.
orglcid Organization Language Code The language code identifier that represents the base language for the organization.
data Optional Data Parameter An optional value that may be passed.

Use below source code to retrieve required values in your Silverlight user control.
xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
if (xrm == null)
{throw new Exception("Failure: Xrm retrieving..."); }
//Set values for ObjectTypeCode, Attribute Name and Entity Name
if (App.Current.Host.InitParams.ContainsKey("type"))
string entityTypeCode = App.Current.Host.InitParams["type"];
if (App.Current.Host.InitParams.ContainsKey("typename"))
string entityTypeName = App.Current.Host.InitParams["typename"];
//returns value of custom parameter “acm_accname”.
if (App.Current.Host.InitParams.ContainsKey("data"))
string attributeName = App.Current.Host.InitParams["data"];

I hope this will be helpful.