CRM Forms and Silverlight Web Resource Integration

CRM Forms and Silverlight Web Resource Integration

When developing a Silverlight Web Resource that has to be added to the CRM form, many time we require to pass data back and forth between the two.

The first step, is that we need to add a couple of classes to the Silverlight application that will allow us to communicate with CRM.
These two classes are the silverlightutility.cs and silverlightextensionmethods.cs, which can be located in the SDK under samplecodecssilverlightsoapforsilverlightsoapforsilverlightsample folder.

This article will demonstrate how to communicate between the applications.

When developing a Silverlight Web Resource that has to be added to the CRM form, many time we require to pass data back and forth between the two.

The first step, is that we need to add a couple of classes to the Silverlight application that will allow us to communicate with CRM.
These two classes are the silverlightutility.cs and silverlightextensionmethods.cs, which can be located in the SDK under samplecodecssilverlightsoapforsilverlightsoapforsilverlightsample folder.

This article will demonstrate how to communicate between the applications.

The first example shows how to retrieve the first name and the last name fields from the contact entity from.
Notice the use of the dynamic object, and the two methods for retrieving the form data.
A reference to the System.Windows.Browser has to be added to class.

        private void GetNameFields()
        {
            dynamic Xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");

            var firstName = Xrm.Page.getAttribute("firstname").getValue();
            var lastName = Xrm.Page.getAttribute("lastname").getValue();
        }

The next example will show how to call a Jscript function on the form.
The HtmlPage.Window.Invoke method accepts two parameters: the name of the Jscript method and the params collection of object data types.

        
private void CallJSShowNotification(string message)
        {
            HtmlPage.Window.Invoke("ShowCustomNotification", new object[] { message });
        }

The last example will demonstrate how to use the Retrieve method of the IOrganizationService to retrieve data from another entity.
Calling the service methods of the IOrganizationService are performed asynchronously, which requires also the creation of a callback method.
The code examples below will call the retrieve method on the account entity and then in the callback method will assign the retrieved values to the string data type.

        
private void GetAccount(Guid accountId)
        {
            try
            {
                string[] columns = new string[] { "name", "telephone1", "emailaddress1", "websiteurl" };
                ColumnSet columnset = new ColumnSet();
                columnset.Columns = new System.Collections.ObjectModel.ObservableCollection<string>(columns);

                if (service == null)
                    service = SilverlightUtility.GetSoapService();

                service.BeginRetrieve("account", accountId, columnset, new AsyncCallback(GetAccountCallback), service);
            }
            catch (System.Exception ex)
            {
                DisplayMessage("GetAccount", ex.Message);
            }
        }

        
private void GetAccountCallback(IAsyncResult result)
        {
            try
            {
                Entity account = ((IOrganizationService)result.AsyncState).EndRetrieve(result);

                string name = account["name"].ToString();
                string phone = account["telephone1"].ToString();
                string emailaddress = account["emailaddress1"].ToString();
                string websiteurl = account["websiteurl"].ToString();
            }
            catch (System.Exception ex)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    DisplayMessage("GetAccountCallback", ex.Message);
                })
            }
        }

You can also use the IOrganizationService to call other web methods such as RetrieveMultiple, Create, Update, etc…