The SPFile
object has a ForwardLinks
and BackwardsLink
property that contain SPLinkCollection
that you may be able to query.
Add-PSSnapin Microsoft.SharePoint.Powershell -ea 0;
$web = get-spweb '<url>'
$list = $web.lists['Pages'];
foreach($file in $list.items){
$links = $file.ForwardLinks;
foreach($link in $links){
if($link.url -like '*/Documents/*'){
Write-Host $file.name $link.url
}
}
}
You can also use CSOM - which doesn't appear to expose the ForwardLinks
/BackwardLinks
properties, but I was able to get at the HTML content of the page and could search across that as a string.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$weburl = 'https://<URL>';
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = Get-Credential;
$web = $ctx.Web;
$ctx.Load($web);
$list = $web.Lists.GetByTitle('Pages');
$ctx.load($list);
#Note - you can create a specific CAML query to get specific documents as well.
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10);
$items = $list.GetItems($query);
$ctx.Load($items);
$ctx.ExecuteQuery();
foreach($fileItem in $items){
$pageContent = $fileItem['PublishingPageContent'];
If($pageContent | select-string -Pattern '/documents/' -SimpleMatch){
#page has link to documents library
Write-Host "Page has link"
}
}