Replace Content Editor Web Part (CEWP) Links with PowerShell

After a SharePoint Migration with URL change, had to find and replace the links from the SharePoint Content Editor Web Part for all SharePoint sites in a web application. Here is the PowerShell Script to find and replace links from the 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")}

Can we do it for Script Editor Web part? Sure, Simply replace “Microsoft.SharePoint.WebPartPages.ContentEditorWebPart” with “Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart”!

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

4 thoughts on “Replace Content Editor Web Part (CEWP) Links with PowerShell

  • December 11, 2019 at 11:54 PM

    This script is really good. I was just wondering is there a way I can find $oldlinks on all pages and export to csv ?

    Reply
  • May 12, 2016 at 7:59 AM

    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.”

    Reply
    • May 12, 2016 at 11:40 AM

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

      Reply

Leave a Reply