PowerShell Script to Reset the Insert Options in Sitecore

A handy script to reset the insert options of multiple items to the templates standard values.

April 1, 2025

By Arsalaan Pathan

Script Overview for Fixing Broken Insert Options

In a Sitecore solution, there are instances where multiple items have broken insert options. You can resolve this issue by either manually resetting the insert options or deleting the broken links individually. This script streamlines the process, allowing you to reset or remove the broken links in bulk, saving time and effort.

How to Find Broken Insert Options?

Sitecore offers a built-in scanner that scans the databases and generates a report of all broken links. To access it, go to the Control Panel and look under the Reports section for the link titled "Scan the database for broken links.”

It's recommended to rebuild your link database before scanning to ensure it's up to date. Let’s do this first. Click on the Sitecore Launchpad button, this will be on the top left corner in your Sitecore application. On the dashboard click on Control Panel. In the Control Panel, under the Database section, click on the "Rebuild link databases" link. You'll be prompted to choose a database (Master, Web, or Core). Select the appropriate one and click "Rebuild.”

Once that’s complete, let’s scan the database. Click on “Scan the database for broken links”. This runs a scan using the master database. After it is finishes you can re-run the scan for web and core databases if necessary.

Scan the database for broken links

Broken Links Output

The image above displays an example of how the scan results will appear. There could be many items listed here. To easily find them, use your browser's search function and look for “Insert Options,” which will highlight all the links.

Click on any of the highlighted items, and a pop-up will appear with all the details for that item. Copy the template path from the details and keep it for reference.

PowerShell script to reset the insert options in Sitecore 2

Item Details

Where to Store the Script?

Most of the PowerShell scripts are kept under /sitecore/system/Modules/PowerShell/Script Library. I wanted the script to be executed on content tree item, specifically the tenant item, So I added it to below path.

Script_Path

Script Path

I also added a rule which will prevent the script to be executed on any item other than my tenant.

Script_rule

Script Rule

Once the rule is added, right click on the item (tenant item in this case), Go to scripts → Reset Insert Options → Reset To Standard Values.

Script_Specific_Item

Selected Item

How Does the Script Work?

The script uses the template path as input and identifies all items with broken insert options based on that template. Run the script with the template path and click OK.

Script_Popup

User prompt

The script may take some time if there are many items with broken insert options. Once the execution is complete, you will see the image below.

Script_Finished

Successful execution

Click on “View script results” to see the output if needed.

Implementation of the PowerShell Script

$inputProps = @{
    Prompt = "Enter the path of the template: (Eg - /sitecore/templates/System/Media/Media folder)"
}

$valueOfIp = Show-Input @inputProps

# Define the database (master or web, depending on your environment)
$database = Get-Database "master"  # Use "web" for Web database if needed

# Get the template item from the template path
$templateItem = Get-Item -Path $valueOfIp

if ($templateItem -eq $null) {
    Write-Host "No template selected. Exiting script."
    return
}

Write-Host "Selected template: $($templateItem.Name)"

# Get the Standard Values item for the selected template
$standardValuesItem = Get-Item -Path $templateItem["__Standard values"]

# Get insert options from the Standard Values item
$insertOptionsFromStandardValues = $standardValuesItem.Fields["__Masters"]
if (-not $insertOptionsFromStandardValues) {
    Write-Host "No Insert Options found in the Standard Values of template: $($templateItem.Name)"
    return
}

# Get all items of the given template
$items = Get-ChildItem -Path "master:/sitecore/content" -Recurse | Where-Object { $_.TemplateID -eq $templateItem.ID }

# Loop through each item
foreach ($item in $items) {
    # Check if the Insert Options (__Masters) field is not empty
    $insertOptions = $item["__Masters"]
    if (-not [string]::IsNullOrEmpty($insertOptions)) {
        # Split the Insert Options value (if it's a pipe list)
        $validInsertOptions = @()
        $insertOptionsList = $insertOptions.Split('|')

        # Check if the templates standard value has insert options
        if (-not [string]::IsNullOrEmpty($insertOptionsFromStandardValues)) {
            $foundnull = 0
            foreach ($option in $insertOptionsList) {
            # Attempt to get the referenced template item by ID
            $templateRef = $database.GetItem($option.Trim())  # Trim to remove any extra spaces

                # If the template reference doesn't exist, add it to the broken links list
                if ($templateRef -eq $null) {
                    $foundnull = 1
                    break
                }            
            }    

            if($foundnull -eq 1)
            {
                # Save the changes
                $item.Editing.BeginEdit()    
                $item["__Masters"] = $insertOptionsFromStandardValues                                    
                try {
                    $item.Editing.EndEdit()
                    Write-Host "Insert Options reset successfully for item: $($item.Name)"
                } catch {
                    Write-Host "Error resetting Insert Options for item: $($item.Name): $_"
                    $item.Editing.CancelEdit()
                }
            }                
        }
        else {
            foreach ($option in $insertOptionsList) {
                # Attempt to get the referenced template item by ID
                $templateRef = $database.GetItem($option.Trim())  # Trim to remove any extra spaces

                # If the template reference exists, add it to the valid options list
                if ($templateRef -ne $null) {
                    $validInsertOptions += $option.Trim()
                } else {
                    Write-Host "Broken link detected: $option"
                }            
            }

            # Save the changes
            $item.Editing.BeginEdit()

            # If there are valid Insert Options left, update the field
            if ($validInsertOptions.Count -gt 0) {
                $item["__Masters"] = $validInsertOptions -join '|'
                Write-Host "Updated Insert Options for item: $($item.Name)"
            } else {
                # If no valid options left, clear the field (optional)
                $item["__Masters"] = ""
                Write-Host "No valid Insert Options left for item: $($item.Name), cleared the field."
            }    


            try {
                $item.Editing.EndEdit()
            } catch {
                Write-Host "Error updating Insert Options for item: $($item.Name): $_"
                $item.Editing.CancelEdit()
            }
        }         
    } else {
        Write-Host "No Insert Options set for item: $($item.Name)"
    }
}

Write-Host "Finished checking Insert Options for all items of template $($templateItem.Name)"

Final Thoughts on Fixing Broken Insert Options in Sitecore

The PowerShell script simplifies the process of fixing broken insert options in Sitecore by automating the reset or removal of broken links in bulk. This approach saves valuable time and effort, making it easier to maintain the content tree.

Picture of Fishtank employee Arsalaan Pathan

Arsalaan Pathan

Sitecore Developer

Arsalaan is not just a web developer; he's a certified expert in Sitecore and Content Hub development, bringing a unique blend of technical prowess and creative vision to every project. Since diving into the world of web development in 2016, he has continually honed his skills, initially on .NET platforms before transitioning seamlessly to Sitecore in 2020. With a passion for crafting dynamic and engaging digital experiences, Arsalaan is driven by the challenge of pushing boundaries and delivering innovative solutions that captivate audiences and drive results.

In his free time, he often savors the thrill of spontaneous road trips, exploring new destinations and soaking in the sights along the way. Alternatively, you'll find him on the soccer field, eagerly chasing after the ball, his passion for the game evident in the beads of sweat glistening on his brow. Whether traversing open roads or dominating the field, he embraces each moment with gusto and determination.