0

ive currently got a powershell script in sitecore which goes through the Home directory and changes the target value for all general links to "Active browser"

What i would now like to do is also change the target values for all internal links in a link list.

Current script:

New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {

# Get items
$root = Get-Item -Path "master:/sitecore/content/SiteName"
# Get its descendents


$allItems = @( ($root.Axes.GetDescendants() | Initialize-Item))

# Get all
$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 "Linklist")
    {
        [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()
        }
    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()
        }
    }
}
}  
}

Can anyone help? thanks!!

EDIT- see image below for the list type: enter image description here

4
  • Hi @SumitBhatia the list type is "linklist". See my edits in the description :)
    – user13705
    Commented Nov 17, 2023 at 14:41
  • What Sitecore version you are using? It looks like a custom field type. Commented Nov 17, 2023 at 14:45
  • im using sitecore version 10. its basically a list, so im thinking will need to iterate through it - similar to a multi list
    – user13705
    Commented Nov 17, 2023 at 14:50
  • @user13705 please provide an example raw field value of containing more than one link. LinkList is a custom field type, which aims to solve a commonly requested feature of being able to add n number of links without specifying a new field for each link (see stackoverflow.com/questions/5853366/… and sitecore.stackexchange.com/questions/7701/list-of-general-links). If you can share where you got the field from, that will help. IIRC, the LinkList field stores values in XML format. Commented Nov 17, 2023 at 16:26

1 Answer 1

0

As per my understanding, you need to get the Link List field type in a variable. For this, you can create a sample script and target only a single item to access the link list field. Once you get the desired result then create a loop like this in your actual logic given above.

# Assuming $linksList is your list of items containing links
$linksList | ForEach-Object {
    $linkItem = $_
    
    # Access link-related fields 
    [Sitecore.Data.Fields.LinkField]$linkField = $linkItem.Fields[$_.name]

    # Your logic for each link item goes here
    Write-Host "Processing link"

    $linkItem.Editing.BeginEdit()
    $linkField.target = ""
    $$linkItem.Editing.EndEdit()
    
    # Add more logic as needed
}

Or if $linksList is giving you the raw values like ID of the item then get the item in the above logic and use the same code in your script to update the target.

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.