Question

I'm hoping that someone can help me get started on a script that will crawl our Wiki Pages for Wiki Links that point to the same page the link is located on. For example, if I have a Wiki Page called "Testing" that has the Wiki Link [[Testing]] in its contents, I want to remove it. It is my stance that pages should not have links in them that point to the page the user is already on, but some of our Wiki users do not seem to have this same stance. Haha.

Any advice or direction would be intensely appreciated. Thank you.

Was it helpful?

Solution

You mean like... "Recursion", see "Recursion"? ;-)

For an on-prem SharePoint for one wiki:

$web = get-spweb http://yourServer/sites/yourSite
$wiki = $web.Lists["Wiki"]
$wiki.items | 
  foreach { $page = $_.Name; $name = $_.DisplayName; $_ } | 
  foreach { ([regex]'id="\d*::\w*\|').Matches($_.xml) } | 
  foreach {$_.Value.split(';')[5].split('|')[0] } | 
  where {$_ -eq $name } | 
  select {$page} -Unique

This uses a Regular Expression to match the link HTML found in the item's XML property. The wiki link text is found and matched to the item's DisplayName property.

This is the text it is matching:

<p>See&#58; <a id="0&#58;&#58;Recursion|Recursion";




Here's a version that will work with SharePoint on-prem or SharePoint Online. You will need to download and install the SharePoint PNP cmdlets. (https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps)

$URL = "http://yourServer/sites/yourSite"
$wikiname = "Wiki"
$wikipath = "/sites/yourSite/wiki/"
Connect-PnPOnline $url
Get-PnPListItem -List $wikiname -Fields FileLeafRef |
  foreach { $pagename = $_["FileLeafRef"]; $_ } |
  Get-PnPWikiPageContent -ServerRelativePageUrl {$wikipath+ $pagename} |
  where { $_ -like "*$pagename*" } |
  select {$pagename}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top