PowerShell script for Fixing URLs in Content Editor WebPart - SharePoint 2010 & 2013
Hi All,
Today i have faced one issue with my client
Scenario
Change the url in content editor WebPart in all the pages those having static content. In my case i was having 600 pages :-
Example : change the "http://www.google.com" to "http://www.company.com"
Please find the below script to achieve the above
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$OldLink="http://www.google.com"
$NewLink="http://kannansharepointworld.blogspot.com"
#Get all Webs
#$webs = Get-SPWebApplication "http://sharepoint2010:2015/" | Get-SPSite -Limit All | Get-SPWeb -Limit All
$webs = Get-SPSite "http://sharepoint2010:2015/" | 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 -eq "Pages")
{
#Write-Host "Folder Name $($folder.Name)"
#Add the pages to $AllPages Array
$AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
}
}
#Iterate through all pages
foreach($Page in $AllPages)
{
#Write-Host "File Status $($Page.CheckOutStatus)"
#Write-Host "File URL $($Page.ServerRelativeUrl)"
if($Page.CheckOutStatus -ne "None" )
{
}
else
{
$Page.CheckOut()
}
#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
#write-host $webpart.Title + " : " + $webpart.ID
$webPartManager.SaveChanges($webPart);
Write-Host "Replaced a link in $($Page.ServerRelativeUrl)"
}
}
#Write-Host "File Status Ok $($Page.CheckOutStatus)"
#Write-Host "Page URL $($Page.ServerRelativeUrl)"
if($Page.CheckOutStatus -ne "None" -and $Page.ServerRelativeUrl -ne "/default.aspx" -and $Page.ServerRelativeUrl -ne "$($web.ServerRelativeUrl)/default.aspx")
{
$Page.CheckIn("Automatic CheckIn. (Administrator)")
$Page.Publish("Automatically published by Powershell")
#$Page.Approve("Automatically Approeved by Powershell") #Uncomment this line when run this script for Publishing Portal(Type) site collection
Write-Host "Page URL $($Page.ServerRelativeUrl)"
}
}
}
Hope this will helps you - Reach me if any queries
Today i have faced one issue with my client
Scenario
Change the url in content editor WebPart in all the pages those having static content. In my case i was having 600 pages :-
Example : change the "http://www.google.com" to "http://www.company.com"
Please find the below script to achieve the above
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$OldLink="http://www.google.com"
$NewLink="http://kannansharepointworld.blogspot.com"
#Get all Webs
#$webs = Get-SPWebApplication "http://sharepoint2010:2015/" | Get-SPSite -Limit All | Get-SPWeb -Limit All
$webs = Get-SPSite "http://sharepoint2010:2015/" | 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 -eq "Pages")
{
#Write-Host "Folder Name $($folder.Name)"
#Add the pages to $AllPages Array
$AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
}
}
#Iterate through all pages
foreach($Page in $AllPages)
{
#Write-Host "File Status $($Page.CheckOutStatus)"
#Write-Host "File URL $($Page.ServerRelativeUrl)"
if($Page.CheckOutStatus -ne "None" )
{
}
else
{
$Page.CheckOut()
}
#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
#write-host $webpart.Title + " : " + $webpart.ID
$webPartManager.SaveChanges($webPart);
Write-Host "Replaced a link in $($Page.ServerRelativeUrl)"
}
}
#Write-Host "File Status Ok $($Page.CheckOutStatus)"
#Write-Host "Page URL $($Page.ServerRelativeUrl)"
if($Page.CheckOutStatus -ne "None" -and $Page.ServerRelativeUrl -ne "/default.aspx" -and $Page.ServerRelativeUrl -ne "$($web.ServerRelativeUrl)/default.aspx")
{
$Page.CheckIn("Automatic CheckIn. (Administrator)")
$Page.Publish("Automatically published by Powershell")
#$Page.Approve("Automatically Approeved by Powershell") #Uncomment this line when run this script for Publishing Portal(Type) site collection
Write-Host "Page URL $($Page.ServerRelativeUrl)"
}
}
}
Hope this will helps you - Reach me if any queries
Comments
Post a Comment