Monday, March 12, 2012

Replace Text within multiple Wikipages using PowerShell





Script for Sharepoint 2010 Enterprise Wikis!

$logfile = "c:\logfile.txt"
$web = Get-SPWeb "http://intranet/wikiurl"
$list = $web.GetList(($web.ServerRelativeUrl.TrimEnd("/") + "/Wiki%20Pages"))

$search = "search_text"
$replace = "replace_test"


(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace | add-content $logfile
(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace | add-content $logfile
foreach ($item in $list.items)
{

if ($item["Page Content"].contains($search))
{
$item.file.CheckOut();
(get-date).ToString() + " >>> " + $item.name + " checked out" | add-content $logfile
do {write-host -NoNewline .;Start-Sleep -m 10;} while ($item.file.CheckOutStatus -eq "None")

$item["Page Content"] = $item["Page Content"].replace($search ,$replace );
$item.update();
(get-date).ToString() + " >>> " + $item.name + " modified" | add-content $logfile
(get-date).ToString() + " >>> " + $search + " changed by " + $replace | add-content $logfile
sleep 1
$item.file.CheckIn("checked in by administrator");
(get-date).ToString() + " >>> " + $item.name + " checked in" | add-content $logfile

write-host $item.name "modified" -foregroundcolor red
}
}


_________________________________________________________________________________
Script for migrated SharePoint 2007 Wikis to SP 2010!

["Page Content"] has been replaced by ["ows_WikiField"]



$logfile = "c:\logfile.txt"
$web = Get-SPWeb "http://intranet/wikiurl"
$list = $web.GetList(($web.ServerRelativeUrl.TrimEnd("/") + "/Wiki%20Pages"))

$search = "search_text"
$replace = "replace_test"


(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace   | add-content $logfile
(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace   | add-content $logfile
foreach ($item in $list.items)
{
 
 if ($item["ows_WikiField"].contains($search))
 {
  $item.file.CheckOut();
  (get-date).ToString() + " >>> " +  $item.name + " checked out"   | add-content $logfile
  do {write-host -NoNewline .;Start-Sleep -m 10;} while ($item.file.CheckOutStatus -eq "None")

  $item["ows_WikiField"] = $item["ows_WikiField"].replace($search ,$replace );
  $item.update();
  (get-date).ToString() + " >>> " +  $item.name + " modified"   | add-content $logfile
  (get-date).ToString() + " >>> " +  $search + " changed by " + $replace  | add-content $logfile
  sleep 1
  $item.file.CheckIn("checked in by administrator");
  (get-date).ToString() + " >>> " +  $item.name + " checked in"   | add-content $logfile

  write-host $item.name "modified" -foregroundcolor red
 }
}


4 comments:

  1. Hi,

    This is exactly what I'm looking for, however it doesn't actually work, as ows_WikiField is not exposed in the list.

    You cannot call a method on a null-valued expression.
    At C:\Users\mdiorioadmin\Desktop\updateWikiLinks.ps1:8 char:37
    + if ($item["ows_WikiField"].contains <<<< ($search))
    + CategoryInfo : InvalidOperation: (contains:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    It looks like $item is a SharePointListItem that doesn't contain ows_WikiField. That element is much deeper and I'm not sure how to get to it.

    Do you have any idea on how to make the above script work? Thanks.

    ReplyDelete
  2. Dear all,
    Thank you very much for this Infomation!
    I've written this script for a migrated "SharePoint 2007" Wiki.
    It seams to work a little bit different on a "real" SP 2010 Enterprise Wiki!!!

    As soon I've a solution for this problem, I'll post the script again on my blog!

    best regards
    Thomas

    ReplyDelete
  3. Dea all,
    I've found the solution earlier than i thought! :-)
    The FieldName in SP 2010 is "Page Content".
    All I did is to replace the word "ows_wikifield" by "Page Content" and it worked for me!

    best regards
    Thomas

    ReplyDelete
  4. Can you tell me the script for deleting the wiki page

    ReplyDelete