1

I want to write a script to bulk update the link field title and text for all the components of a particular type to Read more but I struggle with figuring out how to update these properties.

Here is a part of my script which takes all the search results items and goes through them to update the link field text and title but after this code is invoked and I try to read the $item.Fields["Link"].Text or Value there is no new value written so I assume that they are not saved correctly.

Could you please point me out what is wrong here?

...
$item.Editing.BeginEdit()
[Sitecore.Data.Fields.LinkField]$linkField = $item.Fields["Link"]
    
if($item.Version.Count -gt 0) {
    Switch ($item.Language){
        "en" {
            # I tried different things like below
            $linkField["Text"] = "Read more"
            $linkField.Text = "Read more"
        }
        "de" {
            ...
        }
      ...
    }
    #Trying to assign the new values to the link field but when I try to read it afterwards does not work
    $item.Fields["Link"].Value = $linkField.Value
}

$item.Editing.EndEdit() > $null

2 Answers 2

0

If you want to set the Title and Text properties of Link. Just update your script like the one below.

$item = get-item 'master:/sitecore/content/Home';

$item.Editing.BeginEdit()
[Sitecore.Data.Fields.LinkField]$linkField = $item.Fields["Link"]
    
if($item.Version.Count -gt 0) {
  
        #update Text Value
        $linkField.Text= "My Text"
        
        #Update Title value. 
        $linkField.Title = "My Title"

}

$item.Editing.EndEdit()

Also, you can set other properties the same way by adding properties. Here are the different properties of Link field you can access.

enter image description here

You can validate them by opening Link details:

enter image description here

Hope it helps!

0

So your code will be like the below.

$item = Get-Item master: -ID "{652F3B26-3474-4C79-8A86-AC70CBC26F91}"

[Sitecore.Data.Fields.LinkField]$linkField = $item.Fields["link"]
    
$item.Editing.BeginEdit()
$linkField.Text = "Your New Value"

$linkField.Value= $linkField.Value
$item.Editing.EndEdit()

And it will update your link field.

enter image description here

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.