A Memento of Ideas from Arun Ramdasan

March 27, 2009

Silverlight Web Part for Sharepoint

Filed under: Uncategorized — Arun @ 9:39 am

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

tag and call the Silverlight content to the webpart,

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

output.WriteLine(”

“);

output.WriteLine(“”);

output.WriteLine(“if (!window.Silverlight)”);
output.WriteLine(“window.Silverlight = {};”);
output.WriteLine(“Silverlight.createDelegate = function(instance, method) {“);
output.WriteLine(“return function() {“);
output.WriteLine(“return method.apply(instance, arguments);”);
output.WriteLine(“}}”);
output.WriteLine(“var scene = new Scene();”);

output.WriteLine(“Silverlight.createObjectEx({“);

output.WriteLine(“source: ‘” + this.Page.ClientScript.GetWebResourceUrl(GetType(), “Arun.Scene.xaml”) + “‘,”);

output.WriteLine(“parentElement: document.getElementById(‘” + this.ClientID+”‘),”);
output.WriteLine(“id: ‘” + this.ClientID + “_ctrl’” + “,”);
output.WriteLine(“properties: {width:’100%’, height:’100%’, version:’1.0′ },”);
output.WriteLine(“events:{ onLoad: “+strLoad+”, onError: null },”);
output.WriteLine(“context: null”);
output.WriteLine(“});”);

output.WriteLine(“

“);

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

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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 )

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

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.