U2U Blog

for developers and other creative minds

Deploying a Silverlight application as an Embedded Resource

While my world stopped turning 10 days ago, technology has not. The coming weeks I will post things I stopped working on because my work for Patrick took too much of my (free) time. By keeping up the work I hope to honor him. Thanks for the many words of support and the condolences.

When integrating Silverlight applications with SharePoint custom development, you have different possibilities where to deploy your silverlight application:

- To the ClientBin folder of your IIS web application: If you deploy in this location, it means that the Silverlight application can be picked up by any SharePoint code that runs in the site collections and sites hosted on the IIS Web Application.

- To the 12\Template\Layouts or in the 12\Template\ControlTemplates folder: deploying here means that you can include all of the deployment steps in your SharePoint Solution.

- To a document library: you can create one central document library within your site collection (or if you want a more narrow scope, for your site) where to drop the XAP files.

I refer to the post of Patrick Tisseghem for more details, advantages and disadvantages of each possibility.

In this post I will show you how you can deploy your Silverlight application as an embedded resource of your web part. In that case the Silverlight application will only be available to that web part.

The first step is to build your silverlight application. I used the Hello Silverlight 2.0 application from the Silverlight BluePrint for SharePoint.

Create your web part

Then create your web part. I used the SharePoint WebPart template for Visual Studio 2008.

Add a reference to the following DLLs:

- System.Web.Silverlight.dll (version 2.0.5.0)

- System.Web.Extensions.dll (version 3.5.0.0)

Add your silverlight application (.xap) to your Visual Studio project. I created a Resources directory for it. Set the properties of the xap to Embedded Resource. Don't forget this because otherwise the Silverlight application will not be compiled as embedded resource.

image 

Open the Web Part class. Before the namespace declaration add the metadata attribute that enables an embedded resource in an assembly:

[assembly: WebResource("EmbeddedSilverlightWebPart.Resources.SL.XAML.HelloSilverlight20.xap", "application/x-silverlight-app")]

Check the correct namespace with Reflector.

Add a class level variable for the Silverlight control. This control resides in the System.Web.Silverlight namespace.

Silverlight silverlightControl;

Override the OnLoad event to check whether a Script Manager already lives on the Web Part page.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    // Script manager instance may appear only once on a page
    ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
    if (scriptManager == null)
    {
        scriptManager = new ScriptManager();
        this.Controls.AddAt(0, scriptManager);
    }
}

Instantiate the Silverlight control in the CreateChildControls method and add it to the Controls collection. The Source property of the silverlight control need to be set to the location of the embedded resource. This location can be detected by using the  this.Page.ClientScript.GetWebResourceUrl method:

protected override void CreateChildControls()
{
    base.CreateChildControls();

    this.Width = new Unit("400");
    this.Height = new Unit("200");

    string xapUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "EmbeddedSilverlightWebPart.Resources.SL.XAML.HelloSilverlight20.xap");
    silverlightControl = new System.Web.UI.SilverlightControls.Silverlight()
    {
        ID = "EmbeddedSlCtl",
        MinimumVersion = "2.0.30523",
        Source = xapUrl,
        Width = new System.Web.UI.WebControls.Unit(400),
        Height = new System.Web.UI.WebControls.Unit(200)
    };
    this.Controls.Add(silverlightControl);
}

Override the RenderContents method:

protected override void RenderContents(HtmlTextWriter writer)
{
    EnsureChildControls();
    if (silverlightControl != null)
        silverlightControl.RenderControl(writer);
}

That's it. Build your code.

Deploy the web part

Make the necessary changes to the .webpart file and the .xml file if you want to. Patrick learned me to always add the web part to a significant group in the Web Part Gallery:

<Elements Id="1eba021c-8a0f-4bf2-8f2e-1c9d8f36bf7f" xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="WebParts" List="113" Url="_catalogs/wp">
    <File Path="EmbeddedSilverlight.webpart" Url="EmbeddedSilverlight.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="Silverlight" />
    </File>
  </Module>
</Elements>

Deploy the web part to your SharePoint site. If you use the Visual Studio 2008 extensions for SharePoint, you only have to enter the URL of your SharePoint site in the project properties and press F5.

Don't forget to configure your SharePoint sites for enabling Silverlight if not yet done.

Add the Web Part to a Web Part page

image

You can download the sample web part here.

Thanks to Stefaan Rillaert for pointing me in the right direction.

Comments (38) -

  • tents

    6/10/2015 8:44:50 AM | Reply

    Red your site publish and beloved it. Have you at any time thought about visitor putting up on other associated weblogs comparable to your weblog?

  • breastcancer

    6/11/2015 12:27:23 PM | Reply

    Pink your blog submit and loved it. Have you at any time imagined about visitor publishing on other associated blogs related to your site?

  • curved walkway pavers

    6/14/2015 1:26:17 PM | Reply

    Great, thanks for sharing this post.Much thanks again. Cool.

  • miracolo per acufeni download

    6/15/2015 5:17:57 PM | Reply

    A big thank you for your post.Really looking forward to read more. Really Cool.

  • cerrajeros alhama de murcia

    6/16/2015 6:24:05 AM | Reply

    Thanks-a-mundo for the article post.Really looking forward to read more. Cool.

  • electricistas madrid centro baratos

    6/16/2015 7:30:53 AM | Reply

    There as definately a lot to find out about this subject. I really like all of the points you made.

  • Rand Spear Attorney

    6/16/2015 9:07:16 AM | Reply

    Very good blog post.Really looking forward to read more. Awesome.

  • cerrajeros en chiva valencia

    6/17/2015 11:37:46 AM | Reply

    Muchos Gracias for your article. Awesome.

  • Travel Choice 2015

    6/17/2015 6:11:24 PM | Reply

    Thanks again for the post.Much thanks again. Much obliged.

  • tom mascarenas

    6/18/2015 3:43:58 PM | Reply

    Im thankful for the article post.Much thanks again. Really Cool.

  • cerrajeros valencia centro

    6/19/2015 1:30:27 PM | Reply

    very nice put up, i certainly love this web website, maintain on it

  • private label manufacturing

    6/22/2015 1:24:41 PM | Reply

    It as not that I want to duplicate your web-site, but I really like the style. Could you let me know which design are you using? Or was it especially designed?

  • Data in June 2015

    6/23/2015 3:33:28 PM | Reply

    Looking forward to reading more. Great post.

  • hot lesbian sex

    6/24/2015 7:37:11 AM | Reply

    Really enjoyed this blog post. Keep writing.

  • blog

    6/27/2015 7:04:15 PM | Reply

    Aw, this was a really nice post. In idea I would like to put in writing like this additionally – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and by no means seem to get something done.

  • for sale

    6/29/2015 5:01:38 PM | Reply

    Really informative article post.Much thanks again. Cool.

  • m88

    7/1/2015 4:08:15 PM | Reply

    very nice post, i certainly love this website, keep on it

  • internet resources

    7/12/2015 8:30:19 PM | Reply

    When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

  • internet resources

    9/6/2015 8:59:58 PM | Reply

    There are some interesting points in time in this article but I don’t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

  • online bahis

    9/16/2015 9:07:42 AM | Reply

    It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  • web links

    9/30/2015 3:10:22 PM | Reply

    you have a great blog here! would you like to make some invite posts on my blog?

  • brisbane cosmetic surgeon

    10/2/2015 1:04:22 PM | Reply

    When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

  • internet directory

    10/15/2015 9:58:07 AM | Reply

    Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  • immigration lawyer Cape May

    10/21/2015 8:15:01 AM | Reply

    An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

  • web index

    10/22/2015 12:10:16 PM | Reply

    Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

  • Pixel Film Studios

    11/6/2015 2:23:01 PM | Reply

    The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  • home designers

    11/8/2015 9:34:23 PM | Reply

    You should take part in a contest for one of the best blogs on the web. I will recommend this site!

  • M88

    11/12/2015 2:33:29 AM | Reply

    This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!

  • m88

    11/12/2015 8:25:34 AM | Reply

    very nice post, i certainly love this website, keep on it

  • sick

    11/24/2015 5:45:53 AM | Reply

    WONDERFUL Post.thanks for share..more wait .. …

  • stupid

    11/24/2015 6:00:08 AM | Reply

    This really answered my problem, thank you!

  • yasa disi porno

    11/28/2015 4:43:13 AM | Reply

    There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game. Both boys and girls feel the impact of just a moment’s pleasure, for the rest of their lives.

  • Tall Tee

    12/7/2015 8:55:08 AM | Reply

    Aw, this was a really nice post. In idea I would like to put in writing like this additionally – taking time and actual effort to make a very good article… but what can I say… I procrastinate alot and by no means seem to get something done.

  • iPhone Ad Blocker

    12/11/2015 11:32:29 AM | Reply

    After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  • Instagram Followers

    12/12/2015 4:14:23 PM | Reply

    There is noticeably a bundle to know about this. I assume you made certain nice points in features also.

  • Mold Inspection

    1/4/2016 7:15:14 AM | Reply

    There is noticeably a bundle to know about this. I assume you made certain nice points in features also.

Loading