Create a custom web part to render silverlight application

Home | SharePoint Development | Create a custom web part to render silverlight application

In my previous post, I’ve discussed about how to leverage the content editor web part to render the silverlight application. In this post, I’d be discussing the steps to create a custom web part to render the silverlight application. The custom web part that renders the silverlight application has more advantages. Because things like attributes of silverlight application can be externalized as web part properties. Moreover security can be applied while rendering silverlight application, because the custom web part has power of code-behind support, rather than content editor web part. The whole idea is to render the silverlight control programmatically inside the web parts(using createchildcontrols method) . Here are the steps mentioned below :-

a)Import required namespace

Add a reference to System.Web.Silverlight.

using System.Web.UI.SilverlightControls; 

b)Programmatically add the script manager control to the page

To render the silverlight control, the ScriptManager object needs to added to the Page. Override the OnLoad method of web part, add the ScriptManager control using the following code-snippet. There needs to be only one instance of ScriptManager object registered with the Page object.

    protected override void OnLoad(EventArgs e)

            //get the script manager associated with the current page
            ScriptManager oscriptManager =

            //check whether the script manager is already registered to the page
            if (oscriptManager == null)
                oscriptManager = new ScriptManager();
                Controls.AddAt(0, scriptManager);



c)CreateChildControls method to render silverlight application

Override the CreateChildControls method and render the Silverlight control.

 protected override void CreateChildControls()

            Silverlight oSilverlight = new Silverlight();
            oSilverlight.ID = “SLWPID”;
            oSilverlight.Source = “∼/ClientBin/YourSilverlightApp.xap”;
            oSilverlight.Width = Unit.Percentage(100);

 Subscribe to my post

One thought on “Create a custom web part to render silverlight application

Leave a Reply