1

I am already able to loop through all of the items with a particular tempalteID and show their name, path, and value of the content field.

Current Code:

$_BlogListingFolder = '/sitecore/content/path...'
$_BlogContentTemplateID = '{F542FC85-053E-...-B81A-F7C1FFB68DCC}'
$reportObjects = @()

$allBlogContent = Get-ChildItem -Path $_BlogListingFolder -Recurse | Where-Object{$_.TemplateID -eq 
$_BlogContentTemplateID };

foreach($v in $allBlogContent){
    $reportObjects += $v
}

$reportObjects | Show-ListView -Property @{Label="Redirect Name"; Expression={ $_.Name } } , @{Label="Path"; Expression={ $_.Paths.Path }}, @{Label="Content"; Expression={ $_.Fields["Content"] } }

The reason I'm doing this is to be able to remove anchor tags with particular hrefs (or parts of hrefs) but keep their text.

Example 1: Remove the anchor tag entirely

<a href="google.com">To Google!</a>

Would turn into:

To Google!

Example 2: Remove part of an HREF

<a href="google.com/someChild/someGrandChild">To Googles Grand Child!</a>

Would turn into:

<a href="google.com/someChild">To Googles Grand Child!</a>

UPDATE---------------------------------------------------------

First, I want to thank Ghanendra Singh and Sreekrishnan for their input!

Here is my final code.

$_BlogListingFolder = '/sitecore/content/../blogs'
$_BlogContentTemplateID = '{F542FC85-053E-...-B81A-F7C1FFB68DCC}'
$items = Get-ChildItem -Path $_BlogListingFolder -Recurse | Where- Object{$_.TemplateID -eq $_BlogContentTemplateID };

foreach($v in $items){
    $fieldValue = $v.Fields["Content"].Value
    $htmlDocument = New-Object -TypeName HtmlAgilityPack.HtmlDocument
    $htmlDocument.LoadHtml($fieldValue)

foreach($x in $htmlDocument.DocumentNode.SelectNodes("//a")) { 
        $href = $x.Attributes["href"].Value
    $outerHtml = $x.OuterHtml
    $innerHtml = $x.InnerHtml
        $a1 = "http://final.localhost"
    
        if($href -eq $null -or $href.Contains("~/link.aspx?")){
                #throws errors if null is not specified 
                #ignores dynamic sitecore links
        } elseif($outerHtml.Contains($a1)){
                $fieldValue = $fieldValue.replace($outerHtml, $innerHtml)

                $v.Editing.BeginEdit()
                $v.Fields["Content"].Value = $fieldValue
                $v.Editing.EndEdit()
        }
    }

foreach($x in $htmlDocument.DocumentNode.SelectNodes("//img")) { 
        $src = $x.Attributes["src"].Value
    $outerHtml = $x.OuterHtml
        $img1 = "/project"
    
        if($src -eq $null -or $src.Contains("~/link.aspx?")){
                #throws errors if null is not specified 
                #ignores dynamic sitecore links
        } elseif($src.Contains($img1)){
                $fieldValue = $fieldValue.replace($src, "")

                $v.Editing.BeginEdit()
                $v.Fields["Content"].Value = $fieldValue
                $v.Editing.EndEdit()
        }
    }
 }
2

You can use the HTML Agility Pack, which is designed for use in .Net code, and can be used successfully in PowerShell as well as in snippet below:

Add-Type -Path 'C:\packages\HtmlAgilityPack.1.4.6\lib\Net40-client\HtmlAgilityPack.dll'    
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($string)
$doc.DocumentNode.InnerText

$string in above snippet shall be value of your field. You can the play around with $doc to access the html tags and get the values from them.

0

You can get all the anchor tags from any field value using this particular script. Once you get all these values you can perform some logic to replace or remove the content that you needed to.

$item = gi -path "Item Path here"
$links = @() 
$fieldValue = $item.Fields["Description"].Value
$htmlDocument = New-Object -TypeName HtmlAgilityPack.HtmlDocument
$htmlDocument.LoadHtml($fieldValue)
foreach($x in $htmlDocument.DocumentNode.SelectNodes("//a")) {                
   $links +=$x.OuterHtml               
}
$links

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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