Tuesday, July 10, 2012

Using the Dynamic Type in Silverlight to Easily Access The Client-Side API in Microsoft Dynamics CRM 2011

This is a neat little trick to be able to easily work with the client-side API in CRM 2011 from within Silverlight using the "Dynamic" type.

1. Add a reference to Microsoft.CSharp from the .NET tab in your project.

2. Now you are ready to go.  The Dynamic type is one that allows you to write your code but allow it to not be evaluated until runtime.  This means that the compiler won't throw an error if your syntax is bad though so you will want to be precise.

The example given below illustrates getting the form type and verifying it is an update form and then if it is we are calling a function from a jscript webresource attached to the form (that is part of a namespace).  I also embedded a little call to get the entity ID in there also.  It makes it hard to remember that you are working in C# sometimes.

dynamic xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
var formType = xrm.Page.ui.getFormType();
string strFormID = xrm.Page.data.entity.getId();
if (formType == 2)
    dynamic sdk = (ScriptObject)HtmlPage.Window.GetProperty("SDK");

- I hope this helps!

No comments:

Post a Comment