Speak out my mind

Menu

How to remove “Insert from Template” option in Sitecore

Let’s face it! You can’t always satisfy business needs in the happy case scenario of doing things where all you need to do is one, two three and voilà everyone is happy. Specific business cases sometimes need workarounds and more line of codes to implement but they are always possible in Sitecore.

One of these cases that we were up to was to remove the “insert from template” option for a certain site in a multi-micro site environment. There are many ways of doing this, some that would affect the entire solution and some that would affect certain users, but what if I need to remove this option even from Sitecore Administrator and do it only for certain templates!

Option 1:

For the first glance, permissions would be the best and perfect option to do this where all you need to do is to remove the read permission from the insert options for certain users. To do this:

From Security tab, select assign and the below screenshot will popup up. Revoke the read permission for any user/role you wish to.

This option will remove the “insert from template” option from the entire site for those specific users/roles and yet we didn’t solve our problem.

Option 2:

You can remove or just relocate the “insert from template” option from /sitecore/content/Applications/Content Editor/Menues/New/Insert from Template. This will serve as option 1 for all users/roles and for the entire site in a single step and having the same conclusion.

Option 3:

Use a command template to implement the same functionality of option 1. To do so you need to navigate to the template you wish to remove the “insert from template” option from and:

Insert a definition item for the command template using the System/Branches/Command Template data template.

In the Command field in the data section add item:addfromtemplate(id=$ParentID).

Finally, move, delete, or deny read access for the Everyone role in the Sitecore security domain to the “Insert from Template” item in the Core database.

As you can see that although you can do it programmatically, you still need to configure the Core database and affect the entire site. Sigh!

Are we running out of options to solve this scenario? …. What if I build how the menu looks like in Sitecore and just remove the “Insert from Template” option, is that possible?

Option 4:

Well, absolutely. You can re-build the menu item for Sitecore just by overriding the ItemNew command. “ItemNew” command is triggered whenever you need to create a new item based on another item (template). The basic idea to solve our problem is

When right-click on a certain item, check whether the current item is created from the template we wish to remove the “insert from template” from.

If this item fits the criteria, check if the current menu list item is the “insert from template” item.

Remove the code for inserting this option.

This way, we can ensure that all users are not able to choose any other template to create items for certain sites than those assigned in the insert options. Moreover, we can apply this to any item in any level of the Sitecore tree without affecting other items or sites.

To implement this you need to:

Create a class that overrides the “ItemNew” command.

Override the GetSubmenuItems function with the code below by:

Retrieve the insert options assigned to the current item.

Check for the “insert from template” option ID and do not add the menu items.

Replace the “ItemNew” command in \Website\App_Config\ Commands.config with the new one.