0

I'm using sitecore9.1.1. I need a way to get a list of all items that has a field of type "General Link" that is set to "External Link" and is not working.

(i.e return all broken external links in my sitecore tree).

Is there a way to do so?

1
  • 1
    Sitecore PowerShell Extensions comes with a Broken Links report which optionally includes external links. Commented Oct 30, 2019 at 11:30

1 Answer 1

6

I guess Sitecore Powershell Extension is a better way to check the external broken links on a general link field, a sample script can be like below -

$startPath = "/sitecore/content/website/home"

function Item-Report($i){
   $t =  $i | Get-ItemField -ReturnType TemplateField  -Name "*" | ForEach-Object{
       if($_.Type -eq "General Link"){
       if($i[$_.Name].contains('linktype="external"')){
           [Sitecore.Data.Fields.LinkField]$field = $i.Fields[$_.Name]

       $Url = $field.Url

        try{
           $HTTP_Request = [System.Net.WebRequest]::Create($url)
           $HTTP_Response = $HTTP_Request.GetResponse()

           # We then get the HTTP code as an integer.
           $HTTP_Status = [int]$HTTP_Response.StatusCode

            If ($HTTP_Status -eq 200) {
              Write-Host "URL is OK! for " $i.Id "and Field" $_.Name
            }
            Else {
              Write-Host "Error or broken link on Item ID" $i.Id "and Field" $_.Name
            }
         }
         catch{
            Write-Host "Error or broken link on Item ID" $i.Id "and Field" $_.Name
         }
       }
    }
  }
}

Get-ChildItem -Path $startPath -Recurse | % { Item-Report $_ }

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.