A quick “find that item” hack

The other day I was having a conversation with a colleague who was bemoaning the lack of a fast way to paste a Sitecore item path into the UI and have the Content Editor change selection to that item. I suggested using the search box or the Navigate button, but apparently they weren’t right. The person wanted to paste a link and have the content tree change without any other clicking about.

So, in the spirit of being a helpful co-worker, I hacked up a quick command extension to solve this problem. It’s trivially simple, but I figured someone else might find it useful, or learn something that helps them…

The problem can be solved with a simple Command:

public class NavigateCommand : Command
{
    public override void Execute(CommandContext context)
    {
        ClientPipelineArgs args = new ClientPipelineArgs();
        args.CustomData.Add("db", context.Items[0].Database.Name);

        Context.ClientPage.Start(this, "Run", args);
    }

    protected void Run(ClientPipelineArgs args)
    {
        if (!args.IsPostBack)
        {
            SheerResponse.Input("Enter a path or id: ", string.Empty);
            args.WaitForPostBack();
        }
        else
        {
            if (args.HasResult)
            {
                string dbName = (string)args.CustomData["db"];
                Database db = Sitecore.Configuration.Factory.GetDatabase(dbName);
                Item itm = db.GetItem(args.Result);

                if (itm != null)
                {
                    Context.ClientPage.SendMessage(this, string.Format("item:load(id={0})", itm.ID.ToString()));
                }
                else
                {
                    SheerResponse.ShowError("Item not found", string.Format("The item '{0}' was not found.", args.Result));
                }
            }
        }
    }
}

When the command is triggered by clicking a button, the code records the database that the context item was found in. (That’s whatever item was selected in the content tree when the user clicked the button triggering our command) The “context” database for commands is always Core, so we need this information because the user might actually be looking at Master or Web when they click the button. This is then passed into the Run() method.

The first time Run() is triggered no postback has occurred, so we need to ask the user to enter the path or ID of the item to select, and we need to wait for that data. The SheerResponse.Input() method shows a simple dialog with a text entry field. If you were feeling fancy you could create a custom dialog using Sheer here, but this basic dialog will do for our needs.

The second time Run() is triggered the postback from the input dialog should have occurred. If it has, and if the postback has provided us with some data, then we can try and make the Content Tree change focus. To do that we want to load the item to verify it exits. That means extracting the database name from the arguments passed in, fetching a reference to the database and then trying to load the item.

If we get an item back from the database, we can use the SendMessage() method to trigger the “load an item” behaviour. This will cause the Content Editor to load the item specified, refresh the content tree so it has focus, and refresh the editor pane to show the appropriate data. And if we get no item back we can show an error using the ShowError() method.

Simple, huh?

To enable the user to trigger our command we need to wire it up using the same old techniques we’ve used for commands in the past. First you have to map the class above to a command string by adding an entry to the Commands.config file with a patch:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <commands>
      <command name="testing:navigate" type="Testing.NavigateCommand,Testing" />
    </commands>
  </sitecore>
</configuration>

(Obviously you need your namespace and dll name in the type attribute above…

Then you need to create a button in the UI to trigger this command. For example, to add it to the “Go to” chunk of the Developer ribbon tab, you can add a new item at /sitecore/content/Applications/Content Editor/Ribbons/Chunks/Goto in the Core database. Using the /sitecore/templates/System/Ribbon/Large Button template add something like:

Navigate Button

Choose a suitable icon, fill in the click event with the command name defined above, and give it whatever name you fancy.

So to test it, switch back to Master and click your new button:

Navigate Dialog

If you enter the path /sitecore/system/Marketing Center/Analytics Filters/Report Filters/Country/Australia and click ok, the content tree will change:

Navigated

Bingo. Happy colleague…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s