Core Blimey! - Sitecore Blog

Improving Sitecore Branches - Part 1

April 22, 2016 | 4 Minute Read

Back in January me and the guys at Kagool took part in the Sitecore Hackathon. It was a great 24 hours with lots of fun, sweat and tears (no blood thankfully) as we tried to build a new feature that could be housed within Sitecore Habitat.

After some pondering we came up with the idea to create a site builder tool. Using Sitecore branch templates and a custom site provider, a content editor could create a new Sitecore site based on Habitat just by using the Sitecore interface. This presented some challenges particularly with using branch templates, this series of posts examine a couple of the issues we came across and how we over came them.

Screen Freeze

When you create Sitecore items from a branch the process is pretty simple, you right click, choose the branch template and a name for your new item and you have your new branch of Sitecore items.

adding gif

This is fine for a small number of items but when you have a large tree of items this causes issues:

unresponsive

When you create items from a branch the code is not run asynchronously and you just have to wait for Sitecore to do it’s stuff. This isn’t a great user experience :(. However I have been told that this has been fixed in the next version of Sitecore (we’re currently on 8.1) - so something to look forward to :)

How did we get around this?

Well, we used the tools Sitecore has to offer and put the code that creates branch items in a Sitecore job.

The only issue here is that the code to create the branch items has to be custom code and not Sitecore’s built-in branch functionality.

Thankfully we already had a Contextual Ribbon set up so that we could provide options for site creation, so the custom code could be fired from a button in the Contextual Ribbon. The same thing could also be done using Command templates, but that’s another blog post :).

Contextual Ribbon

Contextual Ribbons are a great feature where you can build up your own ribbon in the Content Editor and have this only display when you click on certain items.

This can easily be configured in the core database:

Under the Contextual Ribbons folder you need to create a Toolbar item ( /sitecore/templates/System/Ribbon/Toolbar) then a Strip item underneath it ( /sitecore/templates/System/Ribbon/Strip) then a Chunk item under neath that (/sitecore/templates/System/Ribbon/Chunk) and then finally a button underneath that ( /sitecore/templates/System/Ribbon/Large Button)

The set up looks like this:

stripchunk

The button behaves like any other Command button in Sitecore. You can also supply arguments to your commands, which came in useful for specifying the template id for the branch we needed to create.

large button

With the Contextual Ribbon wired up all we need now is the code to run the job.

Progress?

If you take a look around at some of the code that’s being fired in the Content Editor there’s some useful stuff going on.

For example when you perform some long running operations like copying and moving large numbers of items Sitecore pops up a progress box informing you of what’s going on. This is exactly what we needed for the creation of our branch items.

If you look closely there is a ProgressBox class within Sitecore that can handles the UI and the creation of the Sitecore Job in an async manner using the ExecuteSync method. Nifty!! It looks something like this.

The ExecuteSync method also has an action and a post action as arguments so after the job is run we can get the method to perform an action. When a branch is created from a branch template nothing happens afterwards, and you are still on the top level node where you created the branch. I wanted to change this so that the new branch got the focus after creation. To do this all I had to do is pass the method used for navigating to the new item in the postaction argument. As part of the creation of branches I also needed to prompt the user for a site name, so I needed a dialog box before the progress box was shown. This meant that I couldn’t use the ExecuteSync method due to issues with post backs so some customisation was needed.

So here is the full code I used to prompt the user for a site name, create the new branch and then focus the content editor to the new branch.

Running this from the top the user experience is now much better. The user can click the create site button, the user can enter their site name and then they can be informed of the progress of the site creation.

dialog

SiteCreatorDialog

Job done! Stay tuned for more fun with branches in the next post.