1

I would like to update the anchor link text, if the anchor link Href match with particular domain/url. Do we have any sitecore powershell script to update the same?

2 Answers 2

1

I have written this script by using this example.

This is my Sitecore item with an RTE field and some anchor tags.

enter image description here

Now, this is the script I used, and if you want you can modify this as per your needs.

# Get your item here by providing ID
$rootItem = Get-Item -Path "master:" -ID "{56305117-8C9C-4C08-96D5-BB97A8E27419}"

# Regex pattern to check if the value contains any anchor
$anchorTagPattern = '(?is)<a[^>]*>(.*?)<\/a>';

# Get your RTE Field 
$richTextContent = $rootItem.Fields["DB"].Value;

# Match parttern with your RTE field value
$anchorTagPatternMatches = [regex]::Matches($richTextContent, $anchorTagPattern);

# If Pattern matches 
if(![string]::IsNullOrEmpty($anchorTagPatternMatches)) {

# Create a loop and it will give you all the anchors in your RTE
ForEach ($patterns in $anchorTagPatternMatches) {

    # Check if anchor contains the text you want (mention your text)
    if($patterns -like '*testlink*')
    {
       
        # Keep that anchor in new variable by replacing text with your new value
        $newValue = $patterns -replace "testlink", "newLink"
        
        # Replace this new anchor with old anchor in your RTE field 
        $richTextContent = $richTextContent.Replace($patterns,$newValue);
       
        # Update your RTE with new value
        $rootItem.Editing.BeginEdit();
        $rootItem.Fields["DB"].Value = $richTextContent;    
        $rootItem.Editing.EndEdit();
    }
}
}

And the output is this. It has updated two anchors that match my value.

enter image description here

2
  • Hi @Sumit, This is something I have expected. I will check on my end. Thanks.
    – Astle
    Jun 16, 2022 at 11:19
  • 1
    @Astle Please accept the answer if this resolves your issue. Thanks Jun 16, 2022 at 11:32
0

You can use below script to update href value in Sitecore rich text field. In below powershall script I replaced value in Multi-Line Text field. I replaced ifarme from object.

$root = gi master:// -id "{11111111-1111-1111-1111-111111111111}"
$items = $root | Get-ChildItem -r | ForEach-Object{$item = $_; $_.Fields  | ?{$_.TypeKey -eq "Multi-Line Text"} }
foreach($i in $items){
   
  
   if ($i.Value -Like '*iframe*' -or $i.Value -Like '*src*') 
   {
         $replaced = $i.Value -replace 'iframe', 'object'
          $replaced = $replaced -replace 'src', 'data'
        $i.Item.Editing.BeginEdit()
  
    $i.Item.Fields[$i.Name].Value = $replaced
    $i.Item.Editing.EndEdit()
    Write-Host $i.Item.Name  "-Test"  $i.Name
    # Write-Host $i.Item.Name  "Test"  $i.Name
   }
   
}

 

Write-Host "Updated " $count " Found Values"

3
  • Hi @Rishiraj, Thanks for your reply. Some rich text field might have multiple anchor links and different Href. In that scenario, I have to replace particular anchor link text which is matched to the particular domain.
    – Astle
    Jun 16, 2022 at 9:14
  • how many domains you have, if you have only two or three in that case you can add conditions according to domain name and then replace value. if($i.Value -Like 'domain1.com') { $replaced = $i.Value -replace 'domain1.com', 'object' } if($i.Value -Like 'domain2.com') { $replaced = $i.Value -replace 'domain2.com', 'object1' } Jun 16, 2022 at 11:16
  • Hi @Rishiraj, It is not countable. Replace query we need to do for particular anchor tag. Because sometimes same content/icon we have for all anchors.
    – Astle
    Jun 16, 2022 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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