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!
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:
- Switch to the Core database.
- Navigate to /sitecore/content/Applications/Content Editor/Menues/New/Insert from Template item.
- 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.
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.
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).
- Implement the command template, you can reference this for more details http://sdn.sitecore.net/upload/sitecore6/datadefinitioncookbook-a4.pdf.
- 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?
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:
- Replace the “ItemNew” command in \Website\App_Config\ Commands.config with the new one.
<command name=”item:new” type=”YourProject.InsertFromTemplate.RemoveInsertFromTemplate, YourProject ” />
And that’s it… you can insert new items without the “Insert from Template” option 🙂