Jul 17, 2009

Passing Parameters to a Silverlight Webpart in SharePoint

Kevin King

Kevin King

Default image background

In this blog, I will outline the steps necessary to pass initialization parameters to a Silverlight webpart within SharePoint.  Due to the client-side processing nature of Silverlight applications, it is not easy to get server-side information for use in your Silverlight application during runtime.  Fortunately, leveraging the SilverlightApp.InitParameters property makes it quite straight-forward to pass information on startup..


  1. Define the SilverlightApp1.InitParameters in the webpart’s CreateChildControls method

  2. Extract these parameters in the Application_Startup method in App.xaml.cs and pass it to the page

  3. Extract the information from the Page parameters

Define the SilverlightApp1.InitParameters in the webpart’s CreateChildControls method

In the CreateChildControls method in the webpart.cs class, we want to create our Silverlight application control and add it to the page.  The Silverlight application object has a property called InitParameters.  This is a string that accepts parameters in this format:  “parameter=value”.  Each parameter/value pair is seperated by a comma, like this:  “parameter1=value1, parameter2=value2″.

Here is a code snippet of the entire Silverlight instantiation, manipulation, and addition:

[code lang=”csharp”]protected override void CreateChildControls()



silverlightControl = new System.Web.UI.SilverlightControls.Silverlight();

silverlightControl.ID = “Silverlight App”;

silverlightControl.MinimumVersion = “2.0.31005.0”;

string parameters = “SiteUrl=” + SPContext.Current.Web.Url.ToString() + “, ” +  “Date=” + DateTime.Now.ToString();

silverlightControl.InitParameters = parameters;

silverlightControl.Source = SPContext.Current.Site.Url + “/XAPS/SilverlightApp.xap”;




Increase business process efficiency and collaboration with Microsoft SharePoint

Explore Our Microsoft Consulting Services  →

As you can see, two parameters will be passed to the Silverlight application at startup.  The first is the current site url that the webpart resides in.  This is useful when using SharePoint web services that require the url of the SharePoint site.  It is key that we use the InitParameters property to pass this information, as the Silverlight application runs on the client-side, so accessing the site url within the Silverlight application itself is not possible.  The second is the Date/Time when the application starts up.

Extract these parameters in the AppStartup method in App.xaml.cs

In the Silverlight application in App.xaml.cs, there is a method called Application_Startup with event arguments StartupEventArgs.  These event args have a property called InitParams.  This is the property that is passed to the page when it is instantiated and set as the RootVisual.

[code lang=”csharp”]private void Application_Startup(object sender, StartupEventArgs e)


RootVisual = new Page(e.InitParams);



Extract the information from the Page parameters

In Page.xaml.cs, the original initialization parameters are passed as page instantiation parameters as shown above.  Within Page.xaml.cs, the Page creation method takes the e.InitParams object, which is of type IDictionary<String, String>.  Therefore, we extract this information like so.

[code lang=”csharp”]public Page(IDictionary initParams)


string siteUrl = initParams[“SiteUrl”];

string dateTimeNow = initParams[“Date”];




We now have two string values, the current SharePoint site url and the date/time when the application is started that can be used by the Silverlight application.

Conversation Icon

Contact Us

Ready to achieve your vision? We're here to help.

We'd love to start a conversation. Fill out the form and we'll connect you with the right person.

Searching for a new career?

View job openings