Wednesday, February 18, 2009

Programmatically Updating a SharePoint Content Editor Web Part

I found it to be a bit tricky to programmatically update a SharePoint's Content Editor Web Part. Well, most properties of the web part are not an issue, however the Content property can be a bit tricky. The reason is because it is of type XmlElement. When I first started to look at doing this, I figured I could just modify the XML and call the SaveChanges() method. It is not that simple. If you don't set the Content property explicitly, that property is not marked dirty and therefore is never saved. Or at least that is my theory. The fact is that you must create a new XmlElement and set the Content property equal to it. If you do this you should have no problem.

Below is a sample method that I used to replace content on ALL Content Editor Web Parts that are on the particular SPWeb (based on the url passed). You could loop through all the SPWebs to do a global search and replace as well.

This method is not robust from a functionality standpoint. It is really just to illustrate how to get a reference to a webpart, make a not so straight forward change, and then persist the changes. For example, this method does not work on any pages that you add or that are not default.aspx. This code could be easily modified to do so though.

using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
...

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
 }

6 comments:

Anonymous said...

This post might be also help:

http://www.etechplanet.com/post/2009/03/19/Modify-property-values-of-a-web-part-in-SharePoint-website-programmatically-using-C.aspx

Brent V said...

Hi Avi,

Thank you very much for your feedback. I greatly appreciate it.

One thing to note about the post you reference is actually the obsolete (see http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getwebpartcollection.aspx) way of doing it if you are using Windows SharePoint Services 3.0 or MOSS 2007. The post does however work for Windows SharePoint Services 2.0.

Unless you are using WSS 2.0, it is strongly recommended that you use the new code as noted in my post. Short of that, the code does essentially the same thing.

Thank you again for your feedback.

Sam said...

Thanks a lot. You just saved my 2nd day :-)

Anonymous said...

Thanks for this solution, very helpful

ntrnci01 said...

Thanks for the solution!

Golden Triangle Tour Package India said...

I have read your article; it is very informative and helpful for me. I admire the valuable information you offer in your articles. Golden Triangle Tour Package India