Monday, October 20, 2008

Integrating Silverlight With Your SharePoint '07 Server

I've recently been trying to integrate Silverlight 2.0 Beta into my MOSS '07 site. I really like some of the animations that it does, and the polished look just can't be beat! There's been a few times where I wished I could create a Silverlight navigation system, or a products carousel, or other things that are just flashy and cool! The problem is that there's really no way for the Silverlight client (which, remember, is on the visitor's web browser/OS) to be able to communicate with the SharePoint server directly.

I read a post somewhere about your Silverlight application connecting to the SharePoint server's built-in web services. I tried using this approach, but ended up running into security issues when the Silverlight client attempted connecting to the web service. (I still need to come back and investigate this approach a bit more as it seems to be a more robust way of handling the interaction with the server.)

There is, however, a slick little trick to getting it to work. You create your WebPart the same way you would under any other circumstance, then create an instance of the Silverlight class and set it's Source property to the location of the XAP file on your web server. Next, you need to set the control's InitParameters property to "ctlid=SL_DATA" (I'll explain this part later). Then, create a DIV control called "SL_DATA" and add an XML document to it. Finally, you can either add the Silverlight control to your child controls collection or use the Silverlight class's RenderControl function to write the HTML for the control straight to an HtmlTextWriter.

In your Page.xaml.cs file, change your constructor so it takes a string parameter named "controlid". In the body of the constructor, add the following lines of code:




string xmlstring = string.Empty;
if (controlid != null)
{

    HtmlElement ctl = HtmlPage.Document.GetElementById(controlid);
    if (ctl != null)
      xmlstring = (string)ctl.GetProperty("innerHTML");


}


You might be asking yourself what this does for us? It's allows us to grab an XML document from within the HTML generated by the WebPart, which will contain the SharePoint data that only it has access to. Pretty neat huh?

The few samples I've tried have ended up breaking my dropdown menus on my navigation, but if I can master the Silverlight interaction with SharePoint, I'll probably be replacing those anyway with something cool and slick in Silverlight anyway, so it might not be anything to worry about.

Happy Coding!!


No comments: