I have managed to get a list of items that use a specific template. I now want to add an Insert option to each of those items.

I understand that I can edit the template Insert options and update the Items but this will remove some custom Insert options that have been added to items. I've looked at the examples here but there isn't one to do what I need to do - so perhaps it's not possible.

  • Do you mean adding Insert options to an existing (inherited) list of Insert options on a template's __Standard values? – Dan Sinclair Nov 26 at 18:04
  • I have a content page that someone has removed the inherited InsertOptions from and added some different ones. This is why I can't just edit the template standardvalues and reset the content InsertOptions – Rick Nov 27 at 8:34
  • 2
    Typically, it's best to manage the Insert options at the template level, even if that means creating multiple types of pages. – Dan Sinclair Nov 27 at 12:05
  • @DanSinclair yes I believe so - I think this might be what we need to do rather than what my question is asking which is sounding more and more like a hack now that I'm beginning to delve deeper into Sitecore. – Rick Nov 27 at 12:07
  • Check my answer below for an alternative answer, but I still recommend managing them on the templates' standard values if possible. :) – Dan Sinclair Nov 27 at 12:26
up vote 8 down vote accepted

Use rules engine to add or remove Insert options

If you can't reset the items' insert options fields for whatever reason, you can always use the Insert rules to add or remove insert options with the rules engine.

Note: the best approach for managing insert options is almost always to use the Insert options (__Masters) field on the __Standard values item of the templates. Adding or removing a large number of insert options in this way will affect performance of the Sitecore Content Editor.

To add a rule that manipulates the insert options:

  1. Create a new item of template Insert Options Rule below /sitecore/system/Settings/Rules/Insert Options/Rules
  2. In the Rule field, click Edit Rule to open the Rule Set Editor

    Rule Set Editor

  3. In the Item Information section of the conditions list (the left list), click on the condition where the item template is _specific template_

  4. In the Rule description section below, you should see your new rule appear. Click the specific template link and select your page's template
  5. In the actions list (the right list), click on either add specific insert options or remove specific insert option
  6. In the Rule description section, click the specific link and select the template you'd like to add/remove

    Rule description configured

  7. Click OK

  8. Add a name for your rule in the Name field
  9. Save the rule item

The best option is usually to assign insert options to standard values of your base template but if that is not an option, you can try something like:

$insertOptionToAdd = '{961227F1-2AC1-4346-B833-D9DBED0B7113}'
$items = Get-Item -Path master: -Query "/sitecore/content/Home/MyItems/* @@templatename ='My Template']"

$items | ForEach-Object {    

  $insertOptions = {$_["__Masters"].Split('|')}.Invoke()

  if ($insertOptions[0] -eq "") {
      $insertOptions.RemoveAt(0);
  }

  if (!$insertOptions.Contains($insertOptionToAdd)) {
      $insertOptions.Add($insertOptionToAdd)

      $newInsertOptions = [string]::Join("|",$insertOptions)
      Write-Host "Updating Insert Options of " $_.ID " to:" $newInsertOptions

      $_.Editing.BeginEdit();
      $_["__Masters"] = $newInsertOptions;
      $_.Editing.EndEdit();
  }
}

(Backup your database before running)

  • Yes I believe the standardvalues approach is the correct way but this project seems to have gone non standard which is unfortunate as I'm completely new to Sitecore – Rick Nov 27 at 8:30
  • 2
    I would say Rule Engine is your best option here. – Richard Seal Nov 27 at 13:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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