1

I have a bunch of items with an rich text field from which I need to remove the last line of it's content if it consists of a chain of * (the chains can vary in length, but are always composed by asterisks and there are other lines with chains of asterisks in the content that's supposed to stay). also worth noting that in html is being rendered like so:

<p style="margin-right: 197.5pt;"><span>*<span style="letter-spacing: -0.1pt;"> </span>*<span style="letter-spacing: -0.05pt;"> </span>*<span style="letter-spacing: -0.1pt;"> </span>*<span style="letter-spacing: -0.05pt;"> </span>*<span style="letter-spacing: -0.1pt;"> </span>* * *<span style="letter-spacing: -0.1pt;"> </span>*<span style="letter-spacing: -0.05pt;"> </span>*</span></p>

All items are under the same path but the amount makes editing them manually unviable, is there a way to bulk-edit the values as a txt file (ie remove the last line if it starts with an * or something like it) using a PowerShell script? Thanks for the help

3
  • There are a lot of examples in the Internet. It's easy task to do, google: "sitecore powershell edit field". Examples: sitecore.stackexchange.com/a/10895/16. blogs.perficient.com/2016/05/25/… Feb 3, 2022 at 19:02
  • yes, i know how to edit a field generally. but in this case is not as simple as replacing the value, as i only want to remove that specific line leaving everything else and as far as i've search idk how the rich text values are handled Feb 3, 2022 at 19:12
  • 1
    RT values are HTML so can either use HtmlAgilityPack (parse to object, remove redundant elements and save again) or try with regex which will catch elements with * and remove them (Regex.Replace) Feb 3, 2022 at 22:33

1 Answer 1

2

Code below does what you need.

You set 2 variables first

  • $rootPath for the root item where the script should start from
  • $fieldName it's the name of your rich text field which you want to update

It goes through the whole tree under rootPath, checks if the field you want to clean contains any * character (otherwise no need to proceed for sure) and uses HtmlAgilityPack library to cleanup content of the field.

Sample input:

<div>
    <a href="https://www.skillcore.net" _target="blank">marek musielak</a>
    <p>some text</p>
    <p style="margin-right: 197.5pt;">
        <span>
            *<span style="letter-spacing: -0.1pt;"> </span>
            *<span style="letter-spacing: -0.05pt;"> </span>*<span style="letter-spacing: -0.1pt;">

            </span>*<span style="letter-spacing: -0.05pt;"> </span>*<span style="letter-spacing: -0.1pt;"> </span>* * *
            <span style="letter-spacing: -0.1pt;"> </span>*<span style="letter-spacing: -0.05pt;"> </span>*
        </span>
    </p>
</div>

Sample output:

<div>
    <a href="https://www.skillcore.net" _target="blank">marek musielak</a>
    <p>some text</p>
    
</div>

Powershell script:

$rootPath = "master:\content\home"
$fieldName = "Text"

function RemoveAsteriskNodes {
    param(
        [HtmlAgilityPack.HtmlNode]$node
    )
    $childNodes = [System.Linq.Enumerable]::ToList($node.ChildNodes);
    if ($childNodes.Count -gt 0) {
        $childNode = $childNodes[$childNodes.Count - 1]
        
        if ($childNode.InnerText.Contains("*") -and [System.Text.RegularExpressions.Regex]::IsMatch($childNode.InnerText, "^[\s*]+$")) {
            $childNode.Remove();
        }
        else {
            RemoveAsteriskNodes $childNode;
        }
    }
}

$items = @()
$items += Get-Item -Path $rootPath
$items += Get-ChildItem -Path $rootPath -Recurse

foreach ($item in $items) {
    $fieldValue = $item[$fieldName]
    if ($fieldValue.Contains("*")) {
        $doc = New-Object HtmlAgilityPack.HtmlDocument
        $doc.LoadHtml($fieldValue);
        RemoveAsteriskNodes $doc.DocumentNode
        $newFieldValue = $doc.DocumentNode.OuterHtml;
        if ($fieldValue -ne $newFieldValue) {
            Write-Host $item.Paths.FullPath
            $item.Editing.BeginEdit()
            $item[$fieldName] = $newFieldValue
            $item.Editing.EndEdit() | out-null
        }
    }
}
1
  • thank you! this is exactly what i required Feb 12, 2022 at 12:40

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.