CRM 2011 + Calling Silverlight Function From Javascript.


Instead of a standalone Silverlight web resource inside CRM it would be better if we could make CRM form and the Silverlight web resource to talk to each other and make a more rich application.

In order to extend capabilities that make a two way interaction between Silverlight and CRM we have to use JavaScript as the medium to share data.
A detailed article is available at the following link:

<a href=”http://blogs.msdn.com/b/thecrmguys/archive/2011/06/13/making-up-getting-your-dynamics-crm-forms-and-silverlight-web-resources-talking-to-each-other.aspx”>Link to article on Silverlight interaction with CRM FORM</a>

I am just writing some sample scripts from the above article for brevity.

Invoking Silverlight methods from JavaScript needs the following modification in the Silverlight code:

a) [ScriptableType()] : this attribute should be put on any class that needs to be accessed by JavaScript.

e.g. Sample Code


namespace SampleSilverlight

{

[ScriptableType()]

public partial class MainPage : UserControl

{

public MainPage()

{

InitializeComponent();

}

}

}

b) [ScriptableMember()] : this attribute should be put on any method that needs to be accessed by JavaScript.

e.g. Sample Code


namespace SampleSilverlight

{

[ScriptableType()]

public partial class MainPage : UserControl

{


public MainPage()

{

InitializeComponent();

}

[ScriptableMember()]

public void Add(int a,int b)

{

    //some code

}

}

}

Next you have to register the object as scriptable object in the Silverlight code:


private void MainPage_Loaded(object sender, RoutedEventArgs e)

{

 HtmlPage.RegisterScriptableObject("interactionObject", this);

}

The “interactionObject” will be used in your JavaScript as a variable to access the MainPage class instance.

Once this is done on silverlight next u have to write code in javascript to access this silverlight as webresource and do some additional coding as shown below.

  1. First get the web resource control.
    
    var control = Xrm.Page.getControl('WebResource_SilverlightClient');
    
    
  2. Get the object of the control retrieved.
    
    var silverlightPlugin = control.getObject();
    
    
  3. Next write code to call the C# function written inside the silverlight webresource which we had marked [ScriptableMember].
    
    silverlightPlugin.Content.interactionObject.Add(int a,int b)
    
    

    The statement “c” above is the one that will execute your function in Silverlight. This feature of interaction between Silverlight and CRM form via JavaScript is of immense help for developers.

Advertisements

15 thoughts on “CRM 2011 + Calling Silverlight Function From Javascript.

  1. sergio

    Hello Sayantan,
    I have done a silverlight control to my incident form in CRM 2011.
    I get the next error window ‘onload error:’ without description. I have been debugging this one and I have found out that when I execute this line mySilverlight.Content.interactionObject.myFunction() in my onLoad event form the errors occurs the first time, normally when I have published silverlight web resource which it’s embedded inside crm form.
    Do you know what’s problem could be producing this problem?
    Greetings and congratulations for your web.

    Reply
    1. Srinath

      Hi,
      Even i’m facing similar error, with no description on the exact same line.. did you get any solution for this one? any workaround?

      Thanks !
      Srinath

      Reply
      1. Sayantan Samanta Post author

        Hi Srinath,

        Sorry for the late reply.

        Try the following and let me know::

        1) Try using the Internet Explorer Dev Tool to debug the javascript and check whats matter.

        2) Check if there is any spelling mistake in ur javascript.

        3) Just to mention u dont need any changes in silverlight code to acces upto the “content” object. Any silverlight control as a webresource will have the “Content” object.

        4) Make sure u have added the Silverlight WebResource to the form.

        5) If all above dont solve ur problem. PLease write in ur javascript. I will help based on ur code then.

  2. Atul

    Hi Sayantan,
    I am also getting error on this line “silverlightplugin.content.intereactionObject.MainFunction()”
    it gives error “content” is undefined.

    please suggest me how i resolve the issue.

    Thanks & Regards,
    Atul

    Reply
    1. Sayantan Samanta Post author

      Hi Atul,

      1) Try using the Internet Explorer Dev Tool to debug the javascript and check whats matter.

      2) Check if there is any spelling mistake in ur javascript.

      3) Just to mention u dont need any changes in silverlight code to acces upto the “content” object. Any silverlight control as a webresource will have the “Content” object.

      4) Make sure u have added the Silverlight WebResource to the form.

      5) If all above dont solve ur problem. PLease write in ur javascript. I will help based on ur code then.

      Reply
      1. Sergio

        I’m continues with the same error…
        My code is :
        if (departmentid != null)
        {
        //HandleOnLoad Web Resource
        var wrControl = Xrm.Page.getControl(‘WebResource_SLMultiProblemTypes’);

        if (wrControl == null)
        return;

        var slobject = wrControl.getObject();

        if (slobject != null && departmentid != null)
        {
        slobject.Content.SLMultiLookupIntegration.FetchFillMultiLookup(departmentid);
        }
        }

        The error only occurs on load of the CRM Form… If the form is loaded there’s no problem with that. Any workaround to solve this? I haven’t got any description with the JS Error.

    1. Sergio

      Ok, it could be possible, but then can you tell me if there’s any way to check if my silverlight resource is loaded from IU JS form?

      Reply
      1. Sayantan Samanta Post author

        Keep a “debugger;” just before the line where u are accessing the silverlight object and make sure Internet Explorer is enabled for debugging script, it will then hit the breakpoint and using IE9 developer tool u can check whether your silverlight object is null or not.

  3. Sergio

    Ok, I know that, and slobject is not null and therefore it executes the next sentence and then the error occurs. It’s a strange behavior and it only occurs in the onload event.
    Thanks anyway

    Reply
  4. Pingback: CRM2011 Silverlight interaction -Kevin's Mocha

  5. Ricky

    Xrm.Page.getControl(webresource) in this line the Script gives an error the Xrm.Page Dosent not have the Object getControl method or function

    is there any solution.

    Reply
  6. jashua

    i am getting error like that problem “slobject.Content.SLMultiLookupIntegration” SLMultiLookupIntegration is null when update the form. but firts load the page is success.. what can i do

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s