Tuesday, April 30, 2013

Replace Content Editor Web Part (CEWP) Links with PowerShell

After a SharePoint Migration with URL change, had to find and replace the links from SharePoint content Editor Web Part for all SharePoint sites in a web Application. Here is the PowerShell Script to find and replace links from Content Editor Web Part.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$OldLink="http://sp10.crescent.com"
$NewLink="http://sharepoint2010.crescent.com"

#Get all Webs
$webs = Get-SPWebApplication "http://sharepoint.crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit All
 
#Iterate through webs
foreach ($web in $webs)
{
#Get All Pages from site's Root into $AllPages Array
$AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})
 
#Search All Folders for Pages
foreach ($folder in $web.Folders)
    {
        if($folder.Name -ne "Forms") #Leave "Forms" Folder
        {
            #Add the pages to $AllPages Array
            $AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
        }
    }
  
 #Iterate through all pages
 foreach($Page in $AllPages)
  {
     #Web Part Manager to get all web parts from the file
     $WebPartManager = $web.GetLimitedWebPartManager( $Page.ServerRelativeUrl,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
 
     #Iterate through each web part
     foreach($webPart in $WebPartManager.WebParts) 
      {
        # Get All Content Editor web parts with specific Old Link
        if( ($webPart.Content.InnerText -like '*'+$OldLink+'*' ) -and ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) )
        {
     #Get the Old content from CEWPs
     $OldContent =  $webPart.Content
           $OldContentXml = $OldContent.InnerText

           #Replace the Old Links
     $XmlDoc = New-Object System.Xml.XmlDocument
     $NewContentXml= $XmlDoc.CreateElement("content") 
           $NewContentXml.InnerText= $OldContentXml.Replace($OldLink, $NewLink)
     
           #Set content and Save
           $webpart.Content = $NewContentXml     

           $webPartManager.SaveChanges($webPart);
           Write-Host "Replaced a link in $($Page.ServerRelativeUrl))"
         }
      }
   }
}

Update Content Editor content in Publishing Sites:
We'll have to call Check-Out, Check-in methods on publishing sites. Here is the Script:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebURL ="https://portal.crescent.com"
$OldLink="http://sp10.crescent.com"
$NewLink="http://sharepoint2010.crescent.com"

#Get the web
$Web = Get-SPWeb $WebURL 
#Get Publishing Web
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
#Get Pages Library
$PublishingPages = $PublishingWeb.GetPublishingPages()
 
#Iterate throgh each page
foreach ($Page in $PublishingPages)
{   
#Checkout the page
$Page.CheckOut() 

#Web Part Manager to get all web parts from the file
$WebPartManager = $web.GetLimitedWebPartManager($Page.Url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  
 #Iterate through each web part
 foreach($webPart in $WebPartManager.WebParts) 
  {
    #Get All Content Editor web parts with specific Old Link
    if( ($webPart.Content.InnerText -like '*'+$OldLink+'*' ) -and ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) )
    {
       #Get the Old content from CEWPs
       $OldContent =  $webPart.Content
       $OldContentXml = $OldContent.InnerText
 
       #Replace the Old Links
       $XmlDoc = New-Object System.Xml.XmlDocument
       $NewContentXml= $XmlDoc.CreateElement("content") 
       $NewContentXml.InnerText= $OldContentXml.Replace($OldLink, $NewLink)
  
       #Set content and Save
       $webpart.Content = $NewContentXml    
 
       $webPartManager.SaveChanges($webPart);
       Write-Host "Replaced a link in $($Page.ServerRelativeUrl))"
     }
  }
#Check in and Publish the page  
$page.CheckIn("CEWP Updated")  
$Page.ListItem.File.Publish("CEWP Updated")}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

3 comments :

  1. Hi,

    Getting below error for sharepoint publishing sites. Can you please help on this..
    Error:

    Exception calling "SaveChanges" with "1" argument(s): "The file is not checked
    out. You must first check out this document before making changes."

    ReplyDelete
    Replies
    1. New script provided to update Publishing pages!

      Delete
    2. Its working in publishing sites without any error.Thanks a lot!!

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...