Share via


How to: Create a Gadget for the Home Page

 

Applies To: Windows Server 2012 Essentials, Windows Home Server 2011, Windows Storage Server 2008 R2 Essentials, Windows Small Business Server 2011 Essentials

A gadget is a user control that you can add to the home page. The gadget that you create is stored in an .ascx file. For more information about creating user controls, see ASP.NET User Controls (https://go.microsoft.com/fwlink/?LinkId=178076).

The following code is a starting point for the Gadget.ascx.cs file, which is included in the WebAddIn template:

namespace WebAddIn  
{  
   public partial class Gadget : Microsoft.WindowsServerSolutions.Web.Gadget  
   {  
      protected void Page_Load(object sender, EventArgs e)  
      {  
      }  
   }  
}  

The WebAddin template provides a Gadget.ascx file that you can use to create a new gadget. If your Remote Web Access add-in does not contain a gadget, you can use the item template named Gadget that is provided in the SDK.

Note

If you have not already created a new project, complete the procedure listed in How to: Create a New Project For Your Remote Web Access Add-In.

To add text to the gadget

  1. Open the Gadget.ascx file.

  2. Add the text after the header. For example, to add “Hello World!” to a gadget, use the following code in the Gadget.ascx file:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Gadget.ascx.cs" Inherits="<WebAddIn>.Gadget" %>  
    Hello world!  
    

    Note

    <WebAddIn> is the name that you assigned to the project.

  3. Save the Gadget.ascx file.

  4. Publish the application.

To add an image to the gadget

  1. Open the Gadget.ascx.cs file.

  2. To obtain a reference to an add-in object, you must create an identifier, such as a Guid. The guid is defined by using the following code:

    private readonly Guid AddinGuid = new Guid("59680f55-38b9-4ef6-a2a9-02ae5f627336");  
    

    Note

    The Guid must be unique. For more information about creating a GUID, see Create Guid (guidgen.exe) (https://go.microsoft.com/fwlink/?LinkId=116098).

  3. Get a reference to an add-in object by using the WebAddInInfo class. For example, use the following code in the Gadget.ascx.cs file:

    WebAddInInfo ai = WebAddInInfo.GetCurrent(AddinGuid);  
    

    To use the WebAddInInfo object you must add the following using statement to the class:

    using Microsoft.WindowsServerSolutions.Web.Extensibility;  
    
  4. Add the image by using the following code in the Gadget.ascx.cs file:

    if (File.Exists(ai.GetPhysicalPath("images\\desktop.png")))  
    {  
       this.Image1.ImageUrl = ai.GetVirtualPath("images\\desktop.png");  
    }  
    

    Image1 is an image control that was added to the Gadget.ascx file. To use the File object you must add the following using statement to the class:

    using System.IO;  
    
  5. Save the Gadget.ascx.cs file.

  6. Publish the application.