0

I have a question, basically I have a SharePoint 2007, and I have some particular pages a cross the portal which contain a Content Editor Webpart.

I would like to be able to modify this CEWP, by updating the source of it. Is there a good way or what could be the best way to do that? Maybe powershell, but how then?

I would appreciate any help.

0
public void SearchAndReplaceInContentEditorWebParts(string siteUrl, string oldString, string newString)
 {
  using (SPSite site = new SPSite(siteUrl))
  {
   using (SPWeb web = site.OpenWeb())
   {
    if (web.Exists)
    {
     using (SPLimitedWebPartManager mgr = web.GetFile("default.aspx").GetLimitedWebPartManager(PersonalizationScope.Shared))
     {

      if (mgr != null)
      {
       foreach (Microsoft.SharePoint.WebPartPages.WebPart part in mgr.WebParts)
       {
        try
        {
         if (part.GetType().ToString().Equals("Microsoft.SharePoint.WebPartPages.ContentEditorWebPart"))
         {
          ContentEditorWebPart contentEditor = (ContentEditorWebPart)part;

          // create a new XmlElement and put the results there
          XmlDocument xmlDoc = new XmlDocument();
          XmlElement xmlElement = xmlDoc.CreateElement("MyElement");
          xmlElement.InnerText = contentEditor.Content.InnerText.Replace(oldString, newString);

          // we MUST set the Content property, not a property of Content.
          // For example, changing the InnerXml or InnerText property of 
          // Content will not be saved.
          contentEditor.Content = xmlElement;

          // persist changes to the database
          mgr.SaveChanges(contentEditor);
         }

        }
        catch (Exception ex)
        {
         //logLine("WebPart ERROR: " + ex.Message);
        }

       }
      }
     }
    }

   }

  }// end using
 }

http://justgeeks.blogspot.co.uk/2009/02/i-found-to-be-bit-tricky-to-update.html

the code explained in a nut shell, loop through spweb, find cewp, update cewp via xml putting in current an new string. Finally save using the webpart inherited method savechanges().

it a bit of a hack but it works, i myself would ammend how its looping through to get the cewp on all pages.

as for powershell you can do:

p.s. youll notice its pritty much same code:

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();

http://blog.metrostarsystems.com/2011/09/20/using-powershell-to-update-content-editor-web-parts/

and another way:

http://www.codeproject.com/Questions/179986/Changing-Content-Editor-Web-part-Text-Programmatic

0

You can use the

SPLimitedWebPartManager

from

Microsoft.SharePoint.WebPartPages

namespace to manipulate Web parts on a Web part page

Similar answer found here -

https://stackoverflow.com/questions/485527/how-to-programmatically-update-content-in-a-sharepoint-web-part

0

Depending on the setup. I'd typically host the CEWP contents in a document library with read access to all and use the WP properties to point at that file for its contents. Then you can just update the one file and have it reflected in all WPs across the site collection.

Of course, if you're asking for a retrospective update then the coding options provided are the only realistic recourse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.