Wednesday, July 25, 2012

Retrieve a lookup field value in a Silverlight page as a WebResource in CRM 2011


To retrieve the values  of  a lookup field  or any other field in a  silverlight page, we need to  follow  two steps  i.e
1. Design the  silverlight application  to  retrieve the lookup field value.
And create  a web resource of type silverlight and upload the .xap file.
2. Create  a  webresource in the form customization area and pass the  field name whose value is needed in the silverlight page as  a parameter in the "custom parameter" area and point  to the web resource created in  the step 1.


We  need to  modify the  Application_startup method to captture the parameter  passed from the CRM controls,
Now  in  the Mainpage.xaml.cs we will get  a hold on  CRM object and retrieve the  value of the parameter that  we have passed as below,

dynamic xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");

var fieldName = Application.Current.Resources["InitParm_data"];

// for getting primary contact lookup field

var numAttr1 = xrm.Page.data.entity.attributes.get(fieldName).getValue()[0].id;

In the  same  way  we  can retrieve  different other fields.

we can also set the crm field values from within the  silverlight application  as  below,

dynamic xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
var numAttr = xrm.Page.data.entity.attributes.get("ser_noofusers");//ser_noofusers is  a custom field numAttr.setValue(Math.Round(slider1.Value, 0));

I hope it helps some one