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()
}
}
}