2

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.

5
  • Do you mean adding Insert options to an existing (inherited) list of Insert options on a template's __Standard values? Commented Nov 26, 2018 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
    – Percy
    Commented Nov 27, 2018 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. Commented Nov 27, 2018 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.
    – Percy
    Commented Nov 27, 2018 at 12:07
  • Check my answer below for an alternative answer, but I still recommend managing them on the templates' standard values if possible. :) Commented Nov 27, 2018 at 12:26

2 Answers 2

9

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
6

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)

4
  • 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
    – Percy
    Commented Nov 27, 2018 at 8:30
  • 2
    I would say Rule Engine is your best option here.
    – Richard Seal
    Commented Nov 27, 2018 at 13:00
  • Hi, where is that Invoke method from? I am new to Sitecore Powershell Extension and not sure what that does...
    – TestUser
    Commented Oct 21, 2019 at 9:21
  • It's standard Poweshell: docs.microsoft.com/en-us/dotnet/api/…
    – Mark Lowe
    Commented Oct 21, 2019 at 9:50

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.