0

I have a requirement to only allow users to create child item by given template. I am thinking to create a template called TemplatedFolder with one field called AllowedTemplateIds as Treelist to configure that folder to contain all the allowed template(s) (most of the time it will be just one template).

What type of code should I write to make sure the following:

from the content tree, when user click on insert menu,

  1. allowed template names will be available (this can be done through assign the template to this folder)
  2. remove the option of "insert from template" only for the TemplatedFolder as parent, not impact any other folders

3 Answers 3

3

"Insert from template" option appear just for the admin user. You can not really remove it really easy and I don't recommend it to remove it.

To have a flexible solution I recommend you to use Insert options rules:

enter image description here

Marek has a great post about insert options rules here: https://www.skillcore.net/sitecore/sitecore-insert-options-rules

More info here: https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-manager/insert-rules-and-insert-option-rules.html

-2

You can extend the item:created event and add your own handler to do certain actions based on template type.

  1. Create your custom class with a public method to perform your logic.
public void OnItemCreated(object sender, EventArgs args)
{
    var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;

    Sitecore.Diagnostics.Assert.IsNotNull(createdArgs, "args");
    if (createdArgs != null)
    {
        Sitecore.Diagnostics.Assert.IsNotNull(createdArgs.Item, "item");
        if (createdArgs.Item != null)
        {
            var item = createdArgs.Item;

            // NOTE: you can write here additional custom logic to fulfill your requirement
            if (item.Parent != null && 
                item.Parent.TemplateName == "Your Template"
                )
            {
                // Delete the item, warn user
                SheerResponse.Alert(
                    String.Format("Sorry, you cannot create an items to {0}.",
                                      item.Parent.Name), new string[0]);
                item.Delete();
            }
        }
    }
}
  1. find <events>, <event name="item:created"> and append your handler at the end.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:created">
        <handler type="MyAssembly.MyNamespace.MyCustomEventHendler, MyAssembly" method="OnItemCreated" />
     </event>
   </events>
  </sitecore>
 </configuration>

if everything is correct the event handler will be called when someone creates an item in your specific folder with a different template then it will show the alert and delete the newly created item.

Hope it helps!

-2

I agree with @Vlad that you don't need to remove it but if you really want to do some customization then you can extend/override the ItemNew command.

Here are the steps you need to follow.

  1. Extract the DLL i.e. Sitecore.kernel and check for the class Sitecore.Shell.Framework.Commands.ItemNew.

  2. Override the GetSubmenuItems method by updating the below logic in this method to not add "insert from template" option in the menu items.

    foreach (Item master in Sitecore.Data.Masters.Masters.GetMasters(context.Items[0]))
    {
        MenuItem menuItem1 = new MenuItem();
        // Add your logic
    }
    
  3. Create the patch and inject it with your logic.

So you can make a check only for the particular item or parent and bypass it for any other items to work with the OOTB approach.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.