Extending Sitecore Rocks

Why Extending “Sitecore Rocks”?

When you are doing certain steps more often, then it is better to automate them. So you avoid making errors by doing something too often.

“Sitecore Rocks” has 2 Posiibilities to extend itself. Writing a Plugin or extend it via Codefile in its “Extension” Folder.

A first sample

Imagine a scenario, you need the “Short ID” of an item. For example to use it in an Querystring.

“Sitecore Rocks” provides a contextmenu in its “Sitecore Explorer” with an “Clipboard” item. You will find “Copy Item Path” and “Copy Item ID” in it. Below the last item we want a “Copy ShortID” entry. By executing this “Command” we want as a result the “ShortID” value in our clipboard in order to use it.

Lets rock!

First we create a Codefile file like “CopyShortIDCommand.cs” with one class “CopyShortIDCommand”. This class has to be derived from “Sitecore.VisualStudio.Commands.CommandBase”.

Creating a first Command

Creating a first Command

As you see, the most important properties for your Command are set in constructor.

Text => defines your contextmenu entry
Group => determines in what group your command will be found
SortingValue => as you can guess it is the position in the group

What about the class’ attribute?

You noticed the CommandAttribute above our class. This is for “Sitecore Rocks” it scans the generated Assembly for Commands, Skins or Piplelines. Giving the property “Submenu” it knows that our command is a submenu item in “Clipboard menu”.

Availability of your entry

Now we set up our Command. But CommandBase needs some overrides. First one is “bool CanExecute(object parameter)“.
We have to decide the accessibility of the command here. It is useful only to allow this feature if there is a chance to get an “ShortID” 😉

In our case it is save to return true, because it is a submenu of “Clipboard”. But then i would have nothing to demonstrate. 😛
My CanExecute Method looks like this:

return true is  too easy

return true is too easy

Now do the work!

The second method to be overridden is void Execute(object parameter). Yes right! This method does all the stuff.

Copying the ID to clipboard

Copying the ID to clipboard

As you see, i use the “GetItemHeader” method of the current AppHost’s Server. In the Callback i use the AppHost’s Clipboard instance and use its “SetText” method in order to put the result’s ItemId converted with “ToShortID()” to the system’s clipboard.

Deploy and use it

After the “Sitecore Rocks” installation  a “Sitecore” Folder will be created  in the User’s AppData folder.
In my Win7 location it is located: “C:\Users\%USERNAME%\AppData\Local\Sitecore\Sitecore.Rocks”.

Under “Extensions” create a folder “CopyShortIDCommand“. Put our file there. Next we need “Sitecore Rocks” to know, that this file is included into its assembly creation process. Therefore create another file named “CopyShortIDCommand.extproj” in our folder.

It contains following lines:

<Project>
      <ItemGroup>
            <Compile Include=”CopyShortIDCommand.cs” />
      </ItemGroup>
</Project>

This file is an extensionproject that is built on the fly by “Sitecore Rocks”. When you split your Command over more files, be sure, that your files are included in the project as seen above.

And now?

Open “Sitecore Explorer” in your “Visual Studio” and locate an item. Right click it. Navigate to “Clipboard” and enjoy!!

Yes! We did it!

Yes! We did it!

I am sure, you will find some other useful ideas.

Happy extending

Yours basty

Advertisements