in

Silverlight Rocks!

Resources to get the most out of Microsoft Silverlight

Creating a Silverlight App in a WebPart

Last post 03-27-2008 12:54 PM by cmptr_man. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 03-25-2008 6:20 PM

    • cmptr_man
    • Top 10 Contributor
    • Joined on 03-25-2008
    • Seattle, WA
    • Posts 3

    Creating a Silverlight App in a WebPart

    Hey there.  This is the first time I've run across this site...cool!  My company just allowed me to start working on Silverlight components for some of our internal web projects, and I'm still trying to get up to speed.  So, it's nice to find resources like this out there for me to browse through.

     Maybe someone can help me with a problem I'm having:

    I have a Silverlight carousel in a web part that I'm using in a SharePoint server farm.  The XAP is in a separate project and seems to work fine when I run it from VS 2008.  However, to make it a part of my web part, I'm creating a Silverlight object instance and adding it to the Controls collection for rendering.  However, when I run the web part in a page, nothing shows up...and I don't mean a blank white Silverlight area (like in VS when an error occurs), it's just the HTML page (if you right-click, you get the IE context menu.)  I can't for the life of me figure out what's going on!  I've made sure the XAP is on the server where the source property is set for...in fact, I can take the URL and past it in the browser and it will try to download the XAP file.  It's acting like the control is never getting created.

    If one of you gets a chance, maybe someone could take a quick look at my code and see if anything jumps out at you?
    Thanks in advance!
    -Dan 

    private ScriptManager smgr = null;

    private Silverlight silverlightControl;

    private LiteralControl literalXMLIsland;

    private ArrayList ctrlColl = new ArrayList();

    protected override void OnInit(EventArgs e)

    {

    base.OnInit(e);

    try

    {

    smgr =
    ScriptManager.GetCurrent(this.Page);if (smgr == null)

    {

    smgr = new ScriptManager();

    smgr.Page = this.Page;this.Controls.Add(smgr);

    }

    base.Width = Unit.Pixel(640);

    base.Height = Unit.Pixel(500);

    silverlightControl = new Silverlight();

    silverlightControl.ID = "SilverlightProductSpinner";

    silverlightControl.Source = "/common/silverlight/ProductSpinnerXAP.xap";

    silverlightControl.Width = Unit.Pixel(640);

    silverlightControl.Height = Unit.Pixel(500);

    silverlightControl.Version = "2.0";

    silverlightControl.Windowless = true;

     

    this.Controls.Add(silverlightControl);

    }

    catch (Exception ex)

    {

    DisplayError(ex.ToString());

    }

  • 03-25-2008 7:34 PM In reply to

    Re: Creating a Silverlight App in a WebPart

    Dan,

     Sounds like maybe you don't have the XAP MIME type defined on the server?

     Some info here:

    http://weblogs.asp.net/cschittko/archive/2008/03/03/silverlight-2-0-app-not-starting-fix-iis.aspx

    Let me know if this works for you.

     Thanks, Bill

    Bill Reiss
    Microsoft MVP - Client App Dev
  • 03-27-2008 12:02 PM In reply to

    • cmptr_man
    • Top 10 Contributor
    • Joined on 03-25-2008
    • Seattle, WA
    • Posts 3

    Re: Creating a Silverlight App in a WebPart

    Thanks for the response Bill,

     I had run across a similar article a week or so ago and made sure to add the MIME extension to my 2k3 server.  Just to be sure, though, I went on my server and made sure that the MIME was registered correctly, which it seemed to be.  I've had a Silverlight app running on this server before, but it was in a stand-alone HTML file.  The only difference is that it's in a web part now instead of in an HTML page--and, of course, that it seems the control is being created through Javascript instead of the OBJECT tag the HTML page auto-generates for you in VS'08.

    If I look at the source on the HTML that's generated, I can see javascript that I'm assuming is supposed to create the object:

    <script type="text/javascript">
    //<![CDATA[
    Sys.UI.Silverlight.Control.createObject("ctl00_m_g_a6e61a1d_6d11_4c53_8f7f_d76707ed7146_SilverlightProductSpinner_parent", "\u003cobject type=\"application/x-silverlight-2-b1\" data=\"data:application/x-silverlight-2-b1,\" id=\"ctl00_m_g_a6e61a1d_6d11_4c53_8f7f_d76707ed7146_SilverlightProductSpinner\" style=\"height:500px;width:640px;\"\u003e\r\n\t\u003cparam name=\"InitParams\" value=\"ctlid=PROD_DATA\"\u003e\r\n\r\n\t\u003c/param\u003e\u003cparam name=\"Windowless\" value=\"True\"\u003e\r\n\r\n\t\u003c/param\u003e\u003ca href=\"http://go.microsoft.com/fwlink/?LinkID=108182\"\u003e\u003cimg src=\"http://go.microsoft.com/fwlink/?LinkID=108181\" alt=\"Get Microsoft Silverlight\" style=\"border-width:0;\" /\u003e\u003c/a\u003e\r\n\u003c/object\u003e");
         //]]>
    </script>

    There's also a script block at the bottom of the page that looks like it's supposed to initialize the control:

    <script type="text/javascript">
    //<![CDATA[
    Sys.Application.initialize();
    Sys.Application.add_init(function() {
        $create(Sys.UI.Silverlight.Control, {"source":"/common/silverlight/ProductSpinnerXAP.xap"}, null, null, $get("ctl00_m_g_a6e61a1d_6d11_4c53_8f7f_d76707ed7146_SilverlightProductSpinner_parent"));
    });
    //]]>
    </script>

    All I see is a white area where the control should be.  If I right-click on the area where my Silverlight app should be, I get the standard HTML page context menu, not the "Silverlight Configuration" context menu, so it looks like whatever is supposed to be creating the control isn't doing its job on page load. 

    There's gotta be something I'm doing wrong.  Any ideas?

    Thanks again for your help Bill,

    -Dan

  • 03-27-2008 12:14 PM In reply to

    Re: Creating a Silverlight App in a WebPart

    I don't know much about Sharepoint, does this help at all?

     http://www.ssblueprints.net/sharepoint/

     Looks like there are Silverlight 2 examples out there for Sharepoint. My only guess on top of that is maybe that the path to the xap is wrong? can you hit the url for the xap file directly?

    Bill Reiss
    Microsoft MVP - Client App Dev
  • 03-27-2008 12:54 PM In reply to

    • cmptr_man
    • Top 10 Contributor
    • Joined on 03-25-2008
    • Seattle, WA
    • Posts 3

    Re: Creating a Silverlight App in a WebPart

    Thanks for the link Bill.  Sample 2 from the link you referenced is actually what I'm basing my Silverlight app on...well, a variation of it, at least.

     I can't navigate to the XAP file directly in the browser.  I get a IE error popup that says that it cannot download the file because "the requested site is either unavailable or cannot be found."  However, the interesting thing about that is that if I misspell the file name (instead of "ProductSpinnerXAP.xap" to ProductSpinnerXAP.xapp", I get just your standard 404 error page on the browser--not the popup error I get when I try to go to the actual file path.  So, I'm pretty sure the file is accessible.

    I can't help but think that the XAP file isn't the problem at all.  If I take my HTML page that works and change the path to the XAP file so that it can't load it, the control is still there, it's just empty.  I can right click on it and see the "Silverlight Configurations" context menu.  In the case of my web part, the control isn't even being created.  I think that's the real problem I need to tackle here: getting the control to be created.

    I appreciate your help Bill.  I think I'll fumble around with it some more and try to see what might be causing it to not work and if nothing else, just create an OBJECT tag in my web part because I know it works that way.  If I create a LiteralControl tag and just put the OBJECT string from my HTML page in there, it loads up just fine...it's got something to do with how that Javascript is creating the object instance...maybe it's still a bug over at MSFT.

    Thanks again, cool site.
    -Dan

Page 1 of 1 (5 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems