1

Im writing a powershell script which at the moment is checking an item ID, and if it contains an internal link that is set to anything other then "Active Browser" it will change the Target to this value.

What I would like to do is go through each item in the entire content tree (/sitecore/content/sitename/Home) and also check each child item for all internal links. Again any that are set to "custom" or "Open in new browser" I would like to set the Target to "Active Browser"

Can anyone help with this?

Below is what I have so far

$item = Get-Item master: -ID "{BDC9773C-7329-4C22-9B87-DD74DC25ADAD}"
[Sitecore.Data.Fields.LinkField]$linkField = $item.Fields["insightsLink"]

Write-Host $item.Fields["insightsLink"]
$item.Editing.BeginEdit()
$linkField.Target = ""
$item.Editing.EndEdit()

Write-Host $item.Fields["insightsLink"]

Thank you!

3 Answers 3

2

You need additional check for Internal links in other answers, Here is your final script -

 # Get Home items
$root = Get-Item -Path "master:/sitecore/content/Home"

# Get its descendents
$allItems = @( ($root.Axes.GetDescendants() | Initialize-Item))

# Get all including Home
#$allItems = @($root) + @(($root.Axes.GetDescendants() | Initialize-Item))

$allItems | ForEach-Object {
    # Get the items
    $currentItem = $_

    # Get item fields and create loop
    $currentItem.Fields | ForEach-Object {

        #Check the field type
        if ($_.type -eq "General Link")
        {
            [Sitecore.Data.Fields.LinkField]$linkField = $currentItem.Fields[$_.name]
            # Update target for Internal Links
            if ($linkField.Linktype -eq 'internal')
            {
                $currentItem.Editing.BeginEdit()
                $linkField.target = ""
                $currentItem.Editing.EndEdit()
            }
        }
    }
} 

Hope this helps !

1
#Give the path of the Parent item below:
$items = Get-ChildItem -Path "master:/sitecore/content/Home" -Recurse

foreach($item in $items){
    foreach($field in $item.Fields){
    if($field.type -eq "General Link"){
        $item.Editing.BeginEdit()
        [Sitecore.Data.Fields.LinkField]$linkField = $item.Fields[$field.Name]
        $linkField.target = ""
        $item.Editing.EndEdit()
    }
    }
}
1
  • Welcome! Please explain your solution :) Sep 28, 2023 at 19:53
1

You can do this by creating a recursive loop on the home item. Here is the code you can use and update as per your needs.

# Get all the items recursively under home
$allItems = Get-ChildItem -Path 'master:///sitecore/content/Home' -Recurse

$allItems | ForEach-Object {
    # Get the items
    $currentItem = $_
    
    # Get item fields and create loop
    $currentItem.Fields | ForEach-Object{
        
        #Check the field type
        if ($_.type -eq "General Link")
        {
            # Update target
            $currentItem.Editing.BeginEdit()
            [Sitecore.Data.Fields.LinkField]$linkField = $currentItem.Fields[$_.name]
            $linkField.target = ""
            $currentItem.Editing.EndEdit()
        }
    }
}

Hope this will help.

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.