Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS

Recommended reading 

Using gadget controller options

To define and extend the behavior of gadgets, use the following attributes and interfaces on the controller class.

[Gadget]

To make an ASP.NET MVC controller into a gadget, decorate it with an attribute (and register its assembly as a shell module). The [Gadget] attribute is in the EPiServer.Shell.Gadgets namespace.

Example:

C#
[Gadget]
     public class ElementsController : Controller
       {
         public ActionResult Index()
           {
             return View();
           }
       }

The [Gadget] attribute has a few options for localization or changing title and default actions.

[ScriptResource("Path/To/Script.js")]

You can associate scripts to a gadget by adding the referenced script to the dashboard header. This is usually combined with a gadget client script init method. The path is relative to the shell module in which the gadget exists but also can be app relative (~/) or site relative (/). This attribute is in the EPiServer.Shell.Web namespace.

Example:  

C#
[Gadget(ClientScriptInitMethod = "playground.events.init")]
[ScriptResource("Content/Events.js")]
public class EventsController : Controller

The attribute also supports some additional properties to control the sort index of this particular script and whether the script is located in another module. Multiple attributes are allowed.

[CssResource("Path/To/Style.css")]

You can associate a style to a gadget by adding the CSS reference to the dashboard’s header. The path is relative to the shell module in which the gadget exists but can also be app relative (~/) or site relative (/). This attribute is in the EPiServer.Shell.Web namespace.

Example:

C#
[Gadget]<br />[CssResource("Content/Elements.css")]
public class ElementsController : Controller

The attribute also supports properties to control the sort index of this particular style and whether the style is located in another module. You can have multiple attributes.

IGadgetNotify

When you create gadget, you can use whatever storage you want, but you may need to clean up your data when a gadget is deleted. For example:

C#
[Gadget]
     public class CleanupController : Controller, IGadgetNotify
         {
      // actions
         public void OnDeleted(Guid gadgetId)
        {
      // clean here
         }
       }

[Authorize(Roles = "CmsEditors, CmsAdmins")]

To restrict who can add and use a certain type of gadget, use the [Authorize] attribute and specify which roles are authorized. This attribute is in the EPiServer.Shell.Web.Mvc namespace.

Example:

C#
[Gadget]
[Authorize(Roles = "NoOne")]
public class SecuredGadgetController : Controller

[VerifyGadgetOwner]

[VerifyGadgetOwner] checks that the gadget belongs to the user's dashboard and verifies that a user can use a certain type of gasget. For example:

C#
[Gadget]
[VerifyGadgetOwner]
public class SecuredGadgetController : Controller
Do you find this information helpful? Please log in to provide feedback.

Last updated: Nov 25, 2015

Recommended reading