Hosting Microsoft Live.com Gadgets in your ASP.NET Application

OK, this has to be one of the most asked questions about Microsoft Live.com gadgets (except perhaps the question – “Are Vista Gadgets and Live.com Gadgets the same?”) –

How to host Microsoft Live.com gadgets in my own ASP.NET applications?

Sounds like there must be an easy answer for this one…. not exactly.  Microsoft haven’t published the Live.com back end infrastructure as yet, just the means in which developers can use the Live.com site to host their home-brew gadgets.  MS won’t commit to whether they intend to release the gadget framework, leaving custom portal developers without any way to incorporate the vast array of Microsoft gadgets into their sites.

I’m not one for giving up easily, so I trawled the web, and after some time found the answer I was looking for.  It seems that Microsoft made some changes to their gadget framework back in late 2005, which changed how gadgets are rendered with IFRAMES.  As a side effect, the following URL will render a Live.com gadget in your browser (try it):

http://gadgets.start.com/gadget.aspx?manifestUrl=http://gadgets.donavon.net/gadgets/digitalclock.xml&dash=false&view=Custom

Hosting Live.com gadgets in your web site is a simple case of embedding an IFRAME tag with the above URL (change the manifest URL to the desired gadget of course):

<iframe id=”Iframe1″ src=”http://gadgets.start.com/gadget.aspx?manifestUrl=http://gadgets.donavon.net/gadgets/digitalclock.xml&dash=false&view=Custom”

scrolling=”auto” frameborder=”0″ width=”100%”></iframe>

I went one step further and created a custom web part for ASP.NET 2, which should also work for SharePoint 2007 with very little adjustment, below is the code.  Throw it into a class library, add a reference to your web site, and instantiate the GadgetWebPart control.

Presto!

public class GadgetWebPart : WebPart

{

#region Fields

private string _manifestURL = String.Empty;

private TextBox _urlTB = null;

private Button _submitBtn = null;

private HtmlGenericControl _iframeCtrl = null;

#endregion Fields

#region Properties

/// <summary>

/// Manifest URL of web part.

/// </summary>

[WebBrowsable(), Personalizable()]

public string ManifestURL

{

get { return _manifestURL; }

set { _manifestURL = value; }

}

#endregion Properties

#region Methods

/// <summary>Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.</summary>

protected override void CreateChildControls()

{

// Clear the controls collection.

Controls.Clear();

// Create controls.

_urlTB = new TextBox();

Controls.Add(_urlTB);

_urlTB.Style.Add(“width”, “200px”);

_submitBtn = new Button();

_submitBtn.Text = “Install Gadget”;

_submitBtn.Click += new EventHandler(submitBtn_Click);

Controls.Add(_submitBtn);

_iframeCtrl = new HtmlGenericControl(“iframe”);

Controls.Add(_iframeCtrl);

_iframeCtrl.Attributes.Add(“width”, “100%”);

_iframeCtrl.Attributes.Add(“height”, “100%”);

_iframeCtrl.Attributes.Add(“scrolling”, “auto”);

_iframeCtrl.Attributes.Add(“frameborder”, “0”);

}

/// <summary>Raises the <see cref=”E:System.Web.UI.Control.PreRender”></see> event.</summary>

/// <param name=”e”>An <see cref=”T:System.EventArgs”></see> object that contains the event data. </param>

protected override void OnPreRender(EventArgs e)

{

// Do we have a manifest URL?

if (!String.IsNullOrEmpty(_manifestURL))

{

_urlTB.Visible = false;

_submitBtn.Visible = false;

_iframeCtrl.Visible = true;

_iframeCtrl.Attributes[“src”] =

String.Format(“http://gadgets.start.com/gadget.aspx?manifestUrl={0}&dash=false&view=custom”, _manifestURL);

}

else

{

_urlTB.Visible = true;

_submitBtn.Visible = true;

_iframeCtrl.Visible = false;

}

// Call base version

base.OnPreRender(e);

}

/// <summary>

/// Submit button clicked.

/// </summary>

/// <param name=”sender”>Sender Object.</param>

/// <param name=”e”>Event args.</param>

void submitBtn_Click(object sender, EventArgs e)

{

if (null != _urlTB && !String.IsNullOrEmpty(_urlTB.Text))

_manifestURL = _urlTB.Text.Trim();

if (null != Width && Width.Value > 0)

_iframeCtrl.Attributes[“width”] = Width.Value.ToString();

if (null != Height && Height.Value > 0)

_iframeCtrl.Attributes[“height”] = Height.Value.ToString();

}

#endregion Methods

}

Thanks to Donavon for his post on the following forum thread:  http://microsoftgadgets.com/forums/1478/ShowPost.aspx

6 thoughts on “Hosting Microsoft Live.com Gadgets in your ASP.NET Application

  1. robgarrett

    Persistence – that’s the beauty of ASP.NET web parts, in the above code I mark the manifest URL property with the Personalizable() attribute, which tells ASP.NET 2 that this property is to be persisted.

    The ASP.NET persistence provider takes care of saving state to storage, which by default saves to an MDF file.

    The WebBrowsable() attribute tells ASP.NET that the property is editable and can be seen in the ProprtyGridEditorPart when the web part manager is in edit mode.

  2. Sahil Malik

    Okay .. 🙂

    Well, a gadget host should be self sufficient, it must not rely on ‘start.com’ to host a gadget within itself.

    So I doubt this is the “true gadget” support that MSFT hasn’t declared details on yet.

    Hosting gadgets without such external dependencies, i.e. truly hosting gadgets is a bit more complex IMO.

  3. robgarrett

    Hosting without the need for Start/Live.com – yup I’d agree, however, until MS releases their framework I cannot see any other way of achieving this. I tried reverse engineering the JS code on Live.com, but ran into a brick wall when I found out that MS uses a web service for backend processing.

  4. http://

    This truly is brilliant. However, I’m struggling to find the manifest URLs I need for other gadgets. Can you suggest where I might find some more?

Comments are closed.