When you host a Silverlight application in a HTML page you can access the HTML Document Object Model (DOM) from the managed code, and call managed code from JavaScript. This is possible using the HTML Bridge technology available in Silverlight.
In the same way you can access the xrm objects on a Dynamics Crm form using managed code and vice versa when a Silverlight application is embedded in a Crm form.
In this post I will show you a simple example of how to do this.
Recipe for this example:
- I will create a simple Silverlight application
- Create a Silverlight web resource
- Add the web resource to a Crm form
- Verify
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Stretch">
<TextBox x:Name="InputText" Width="200" Height="23"/>
<Button Content="Go" Width="22" Height="20" Click="Button_Click" Margin="4,0,0,0"/>
</StackPanel>
<TextBox x:Name="InputText" Width="200" Height="23"/>
<Button Content="Go" Width="22" Height="20" Click="Button_Click" Margin="4,0,0,0"/>
</StackPanel>
After having created the Silverlight application let's add the following code to the Button_Click event handler:
add this a reference to the "System.Windows.Browser" to your Silverlight project and the using statement in order to be able to use the HTML Bridge,
using System.Windows.Browser;
private void Button_Click(object sender, RoutedEventArgs e)
{
//Get the xrm object using the dynamic type
dynamic xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
//this is the number of employees attribute
var numEmployees = xrm.Page.data.entity.attributes.get("numberofemployees");
try
{
if (!string.IsNullOrEmpty(InputText.Text))
{
//convert to int the input from SL text box
var employees = Convert.ToInt32(InputText.Text);
//set the attribute on the Crm form
numEmployees.setValue(employees);
}
else
{
//set the attribute on the form
numEmployees.setValue(null);
}
}
catch(System.FormatException fe)
{
HtmlPage.Window.Alert(string.Format("{0}", fe.Message));
}
catch(System.OverflowException ofe)
{
HtmlPage.Window.Alert(string.Format("{0}" , ofe.Message));
}
}
After having added the Silverlight web resource (see this post) we can now embed it in a form (the account form in this example), thje result is shown below:
To test the application enter a value into the textbox and enter "Go", you should see the number of employee populated with the value you have inserted in alternative you can leave the text box empty.
If you try to enter a value that is too large, too small or is not a number an exception will be thrown and a message will be displayed (see below) . Note that the messages are once again shown using the HTML Bridge through the following lines of code:
HtmlPage.Window.Alert(string.Format("{0}", fe.Message));
HtmlPage.Window.Alert(string.Format("{0}" , ofe.Message));
Alternatively we could have used a Silverlight Child window or simple MessageBox.Show("")
Well that's all really! Next time we will see how to call a Silverlight method from Crm form.
Stay tuned.
Luciano.
Hi Luciano,
ReplyDeletei am trying to get CRM form field value in my silverlight application using HTML Bridge as you shown in your code,but i am not able to get the field value.
Below is my code
dynamic xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
var RegardingContact = xrm.Page.data.entity.attributes.get("regardingobjectid");
var abc = RegardingContact.getValue();
I am trying to get "regardingobjectid" field value, it returns me object,,but not value. can you suggest me how i get the value.
Thanks in advance,
Atul