I want to replace an item with another item in a Treelist. Basically, the items I want to replace are broken links. Suppose,

There are two items A and B. A is used in a Treelist field, so I want to replace item A with item B in Treelist by using their ID.

Please let me know the optimal solution. Thanks.

  • I think that you should be able to find almost ready-to-use snippet with google serach. Saerch for replace field value sitecore powershell First result that appear for me: blogs.perficientdigital.com/2016/05/25/… The difference is that William replace url, you will have to adjust it to replace item id. Feb 14 '19 at 16:07
  • @AlanPłócieniak is it possible to replace broken links in treelist with other items?
    – PSS
    Feb 14 '19 at 16:09
  • There is Sitecore admin page that allows removing broken links (stackoverflow.com/a/28738468/6149877). But from what I see you need to replace it with other value, am I right? If you just need to get rid of them, follow the link. Feb 14 '19 at 16:13
  • @AlanPłócieniak I know to remove broken links I can use Sitecore tool, but I have to replace the broken links with other items. I have 4 items[broken] which are selected in more than 100 items in treelist's field. So, here I just want to replace the id of these 4 items in every treelist.
    – PSS
    Feb 14 '19 at 16:19
  • Try the link from first comment then. You will have to edit raw values of an item (geekhive.com/buzz/post/2017/02/…). Links to items are represented in Sitecore as GUIDs (pl.wikipedia.org/wiki/Globally_Unique_Identifier). Once you know GUID you are looking for you can find items and then replace field value. Feb 14 '19 at 16:25

You could try something similar to what I have here with pse and adapt it with your use case if needed. And basically, broken link usually (if not all the time, not quite sure) use {00000000-0000-0000-0000-000000000000} value for the item id. So I play around that to fix my links. I consider this more of a quick fix so at least you don't have to manually fix them all at once but instead fix with a generic item fast, then apply manual fix on item where and when needed...

$rootPath = 'master:/sitecore/path/to/your/parentFolder';
$templateId = '{FFB1DA32-2764-47DB-83B0-95B843546A7E}';

$invalidItemLinkValue = '{00000000-0000-0000-0000-000000000000}';

$validGenericLinkItemValue = '<link id="{8AD423E4-5EA3-4C8C-ADC9-45CDB869BCE1}" querystring="" text="" title="" class="" linktype="internal" />'
$validGenericItem = '{F4D50806-6B89-4F2D-89FE-F77FC0A07D48}';

Set-Location $rootPath;

Get-ChildItem -Recurse | 
ForEach-Object {
    if ($_.TemplateId -eq $templateId) 
        $treeListField = $_.Fields["YourTreeListField"].Value;
        if (![string]::IsNullOrEmpty($treeListField))
            $itemsGuidFromYourTreeListField = $treeListField.Split('|').Trim();
            if($itemsGuidFromYourTreeListField -ne $null)
                foreach($itemGuid in $itemsGuidFromYourTreeListField)
                    # ref : https://doc.sitecorepowershell.com/appendix/provider/get-item
                    $currentItem = Get-Item -Path "master:" -ID $itemGuid -ErrorAction SilentlyContinue;
                    if([string]::IsNullOrEmpty($currentItem.Fields["LinkField"].Value) -or $currentItem.Fields["LinkField"].Value -like $invalidItemLinkValue)
                        # ref : https://doc.sitecorepowershell.com/working-with-items
                        # if you want to update the actual link reference with a general link...
                        $currentItem.Fields["LinkField"].Value = $validGenericLinkItemValue;
                        $currentItem.Editing.EndEdit() | Out-Null;

                        # a) if you want to remove the reference from the treelist and use a new "generic" item that is hardcoded here...
                        $treeListField = $treeListField.Replace($itemGuid, $validGenericItem);

                # b) if you want to update the actual link reference with a general link...
                $_.Fields["YourTreeListField"].Value = $itemsGuidFromYourTreeListField;
                $_.Editing.EndEdit() | Out-Null;

The script below should help you.

Modify the variables as required. You may have to run the script a few times, changing the values as required.

function ToArray {
    Begin { $output = @(); }
    Process { $output += $_; }
    End { return , $output; }
$ParentItemId = "<Add Id of the Parent Item>"
$Language = "en"
$TemplateName = "<Add Template Name of the item to be filtered>"
$FieldName = "<Add Field Name of the Treelist field>"
$OldFieldValue = "<Add Broken Link Item ID Value>"
$NewFieldValue = "<Add Item Id Value to be replaced with>"

$AllItems = Get-ChildItem $ParentItemId -Language $Language -Recurse | Where-Object { $_.TemplateName -eq $TemplateName } | ToArray

foreach($i in $AllItems)
    $oldValue = $i.Fields[$FieldName].Value
    if($oldValue -Match $OldFieldValue)
        $i["FieldName"] = $oldValue -Replace $OldFieldValue, $NewFieldValue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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