SharePoint – Replace Link on Wiki Page


Hello everyone. A while ago I was asked by a colleague if there is a way to replace an old link with a new one in many Wiki pages. He asked me because he needed to replace an old link in a library with hundreds of pages. Pretty hard work if you need to do this manually but fortunately there is a way to do this automatically by using PowerShell.

In the below screenshot you can see one of the example pages which are created when you create a new Wiki Library. And we will try to replace the link I marked with a new one.

sharepoint_wiki_link_before

To do so we run PowerShell as Administrator (on one of your SharePoint servers) and run my script WikiPageReplaceString.ps1.

sharepoint_wiki_replace_string_script

As you can see the script successfully replaced the link with the new value.

sharepoint_wiki_link_after

And here is the script code:

Param([parameter(Mandatory=$true)][alias("Url")]$SPWebUrl, [parameter(Mandatory=$true)][alias("ListName")]$SPListName, [parameter(Mandatory=$true)][alias("Search")]$SearchString, [parameter(Mandatory=$true)][alias("Replace")]$ReplaceString)

#Add SharePoint PowerShell SnapIn if not already added
if((Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Get SharePoint website object
$SPWeb = Get-SPWeb $SPWebUrl
#Get SharePoint list object
$SPList = $SPWeb.Lists[$SPListName]

#Iterate through each item in the Wiki Library
foreach($SPListItem in $SPList.Items)
{
#Check if $SPListItem contains $SearchString
if($SPListItem["ows_WikiField"] -and $SPListItem["ows_WikiField"].contains($SearchString))
{
#Check out page
$SPListItem.File.CheckOut();

#Replace $Search_String with $Replace_String and update item
$SPListItem["ows_WikiField"] = $SPListItem["ows_WikiField"].replace($SearchString, $ReplaceString);
$SPListItem.Update();

#Check in page
$SPListItem.File.CheckIn("Checked in by System.");
}
}

The script basically does the following:

It will iterate through each item in your Wiki library and check if the string you are searching for exists in the content. If the string is present it will check out the item and replace the string with the new value you entered. After it successfully replaced the string the script will check in your item again and repeat theses steps for the next item until all items have been checked.

Pretty simple, right? So next time you need to update your Wiki libraries you will have this script to do the work for you. I hope you liked my post and as always you can download my script from here.

Sources:

http://blogs.technet.com/b/sp/archive/2013/05/29/http-www-blogger-com-blogger-g-blogid-8070685728411204795-editor-target-post-postid-1574709978165689298-onpublishedmenu-overviewstats-onclosedmenu-overviewstats-postnum-38-src-postname.aspx

http://sharepoint.stackexchange.com/questions/32945/sharepoint-2010-custom-wiki-library

Advertisements
Tagged ,

4 thoughts on “SharePoint – Replace Link on Wiki Page

  1. Olen says:

    We absolutely love your blog and find most of your post’s
    to be exactly I’m looking for. Does one offer guest writers to write content for yourself?
    I wouldn’t mind publishing a post or elaborating on most of the subjects you write with regards to here.
    Again, awesome blog!

  2. Eric says:

    This scipt does not Change the hyperlink… just changes the text string displayed.

  3. Eric says:

    if you have the required additions to the script to change the actual URL that would be great! – that is the use case I am trying to accomplish. No Luck so far.

    • theezitguy says:

      Hello Eric. Thank you for commenting on my post. Um, actually this script does replace the actual URL. I used it multiple times to replace Links on Wiki pages of customers. All you need to do is to put the URL you want to replace into the $SearchString variable (e.g. http://www.bing.com) and you need to put the new URL into the $ReplaceString variable (e.g. http://www.google.com). That should do the trick.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: