Have you ever been in a situation where you needed to programmatically update the content in Content Editor Web Part (CEWP)? Maybe you added some HTML that is breaking your page or maybe you’re moving your content to another domain and you have some hard coded links in your CEWP’s.
Well it turns out that it isn’t really that hard. In my situation we were working with the latter scenario above and within our environment we have many administrators who have created hard coded links within CEWP’s and in order to streamline the upgrade process I thought I would create a PowerShell script.
In the below script, I am essentially referencing the page directly and replacing the domain in the URL’s. My original script actually iterates the entire site collection and also updated other Web Parts, but it turned out to be a lot of code and I wanted to keep this simple.
param([string]$url, [string]$oldUrl, [string]$newUrl, [switch]$checkOnly)
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
$site = New-Object Microsoft.SharePoint.SPSite($url);
$web = $site.RootWeb;
$requiresUpdate = $false;
$webPartsToUpdate=@();
Write-Host("");
$webPartManager = $web.GetLimitedWebPartManager("default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$webParts = $webPartManager.WebParts;
foreach($webPart in $webParts)
{
$tempCheckString = "*" + $oldUrl + "*";
#Update Content Editor Web Part Content
if ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart] -and $webPart.Content.InnerText -like $tempCheckString)
{
try
{
# Used as a check before committing updates
if ($checkOnly)
{
$o = new-object psobject;
$o | Add-Member -MemberType noteproperty -Name Title -value $webPart.Title;
$o | Add-Member -MemberType noteproperty -Name Content -value $webPart.Content.InnerText;
$webPartsToUpdate += $o;
}
else
{
Write-Host("Updating: " + $webPart.Title);
# Load Old Content
$oldXmlElement = $webPart.Content;
$oldXmlContent = $oldXmlElement.InnerText;
# Create new XML Element and update text
$xmlDoc = New-Object xml;
$newXmlElement = $xmlDoc.CreateElement("NewContent");
$newXmlElement.InnerText = $oldXmlContent.Replace($oldUrl, $newUrl);
# Update content and save
$webPart.Content = $newXmlElement;
$webPartManager.SaveChanges($webPart);
Write-Host("");
}
}
catch
{
"Error: $_";
}
}
}
if ($checkOnly)
{
Write-Host("Web Parts Requiring Update:");
$webPartsToUpdate | format-list;
}
else
{
Write-Host("Updates Complete");
Write-Host("");
}
$web.Dispose();
$site.Dispose();
So to use this, you basically just need to copy the above source (navigate your mouse to ‘Line 1′ at the top of the code script above to bring up the ‘copy’ and ‘print’ options for the code), paste it into a text file, save as Update-HardCodedLinks.ps1, and finally run it. An example initial run should look something like this:
./Update-HardCodedLinks -url http://www.newURL.com -oldUrl www.oldURL.com -newUrl www.newUrl.com -checkOnly
If that goes well, remove the checkOnly switch and your content should get updated.
Finally, note that this targets SharePoint 2010, but with a few changes this should also work for SharePoint 2007. Enjoy!
The post Using PowerShell to Update Content Editor Web Parts appeared first on MetroStar Systems Blog.