Friday, March 27, 2009

Silverlight Web Part for Sharepoint

In this post, we are going to see integrating the webpart with Silverlight contents on SharePoint Site.

For that, we need to combine all required java script and Xaml files (used to display the Silverlight content) in to single assembly without any dependent files. It makes sense to embed the Xaml and java script file as a resource and reference it in programming using the WebResource.axd handler mechanism for extracting embedded resources.

1. Create a Webpart project and create or add the required java script and Xaml files to the project.
Include some files Silverlight.js, Scene.js and Scene.xaml


2. Set the BuildAction property to “Embedded Resource” in properties window for each java script and Xaml.
This will use to include the files as Resources in an assembly.

3. Add the assembly-level attribute System.Web.UI.WebResource to grant permission for these resources to be served by WebResource.axd and to associate MIME type for the response.

[assembly: WebResource("Arun.Silverlight.js", "text/javascript" )]
[assembly: WebResource("Arun.Scene.js", "text/javascript")]
[assembly: WebResource("Arun.Scene.xaml", "text/xml")]

Now JavaScript and Xaml files are compiled into my assembly as embedded resources.

4. Now, we can use the RegisterClientScriptresource() method of the Page.ClientScriptManager class to rendered the page with the referenced files.

this.Page.ClientScript.RegisterClientScriptResource(GetType(), "Arun.Silverlight.js"); this.Page.ClientScript.RegisterClientScriptResource(GetType(), "Arun.Scene.js");

Include the above lines in the PreRender method to register the javascript files for the webpart.

5. Add the following lines to the RenderWebPart method to host the < div > tag and call the Silverlight content to the webpart,

string strLoad = "Silverlight.createDelegate(scene, scene.handleLoad)";

output.WriteLine("
");

output.WriteLine("
");

The method GetWebResourceUrl(GetType(), "Arun.Scene.xaml") used to retrieve the Url of the Xaml file from WebResource.axd.



No comments: