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=CustomHosting 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):
scrolling="auto" frameborder="0" width="100%">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