Sitecore Blog: @sitecorejohn blog

Sitecore Rocks Extensions

By John West, June 30, 2011 | Rating:  | Leave a comment

Sitecore logo over rocks

This blog post contains information about extensions for Sitecore Rocks, the Visual Studio extension for developers working with the ASP.NET CMS. For more information about Sitecore Rocks, see my blog post Introducing Sitecore Rocks. For more information about extending Sitecore Rocks, see my blog post Sitecore Rocks Plugins and Extensions.

Extensions are light-weight plugins compiled automatically in the background. Extensions provide the easiest mechanism for extending Sitecore Rocks. To implement an extension, you don't even need to create a Visual Studio project - you just write some code, and Rocks applies it automatically. You can later convert an extension to a project.

Before you can create an extension, you need to enable extensions. To enable extensions:

  1. In Visual Studio, click the Sitecore menu, and then click Extensions... The Extensions (plural) dialog appears.
  2. In the Extensions dialog, select the Enable Sitecore Rocks Extensions in Context Menus checkbox.


Creating an extension is incredibly easy. To create an extension:

  1. In Visual Studio, right-click on an object of the type that brings up the context menu that you want to extend, such as the header in the item editor or a connection, database or item in Sitecore Explorer.
  2. Click Tools, then click Sitecore Rocks Extensions, and then click New Sitecore Explorer Command. The Extension (singular) dialog appears.
  3. In the Extension dialog, enter a name for the extension (this text will appear on the context menu).


Sitecore creates a code file for the extension. That's it; if you right-click the same object again, your extension appears towards the top of the menu. If Sitecore Rocks cannot compile the code, it writes errors to a file named after the project in a directory named after the project under the AppData\Local\Sitecore\Sitecore.Rocks\Extensions folder for the user.

The default code for a new extension class provides a stub that simply pops up an alert with the name you entered for the extension. The constructor defines three key properties of the extension. The class inherits from the Sitecore.VisualStudio.Commands class and is decorated with the [Command] attribute. The default constructor sets three key properties:

  • Text: Appears on the Visual Studio context menu.
  • SortingValue: Controls the order of extensions appearing on the context menu.
  • Group: For grouping commands between separators. To build the menu, Sitecore Rocks first sorts commands by SortingValue, then while iterating each, if the Group property differs from the Group property of the previous command, Rocks inserts a separator.

The default code also implements the CanExecute() method that controls on which menus the extension appears. Basically, each time it needs to generate a context menu, Sitecore Rocks passes a context argument to this method in each command class, and includes only those commands for which this method returns true. This method typically checks the type of the parameter, the number of items selected in the user interface and/or the type of selected item(s) to see if the command is relevant to the calling context.

The Execute() method contains the body of the implementation. Sitecore Rocks passes the same parameter to this method that it passed to the CanExecute() method. For example code, see existing plugins in the repository (in Visual Studio, click the Sitecore menu, and then click Plugins...).

To convert an extension to a Visual Studio project:

  1. In Visual Studio, click the Sitecore menu, and then click Extensions... The Extensions dialog appears.
  2. In the Extensions dialog, click To VS Project. The Convert Extension dialog appears.
  3. Enter values in the Convert Extension dialog, and then click OK.


For more information about working with Sitecore Rocks Visual Studio projects, see http://vsplugins.sitecore.net/Creating-Visual-Studio-Projects.ashx.

Tags: API, Architecture, Infrastructure, Integration

*
*
*

Learn More with Sitecore

Newsletter
*

N. Vancouver School District | Read Case Study >

To us, Sitecore has been more than just a CMS, but a development platform. We are thrilled with the depth and sophistication of the Sitecore environment.

- Brian Fairholm, North Vancouver School District