Global update to all instances of a SharePoint Content Editor Web Part

Author by Steve Borgwardt

Recently we had a scenario come up for a client in which they had the need to update the URL of the Content Link within all of their Content Editor Web Parts (CEWP) within their existing SharePoint environment while migrating from 2007 to 2010. This scenario could also be helpful if you have several content editor web parts that point to a specific server or page in which the host or file name has changed and you need to update all references to this. For this particular scenario we had to go through all the SharePoint web applications, site collections and sites within the SharePoint farm so this code will work if you only need to update one site or an entire SharePoint server farm. Below is a basic Windows console application that you can utilize for a one-time update of the Content Link (or any other property you would like to change including the text within the CEWP) for all content editor web parts within your SharePoint environment. First, you need to update these two variables for the URL you need to replace within the content link field, and the new value you want to use for that field. private static string _originalLink = @"http://server1.oldlink.com/test.txt"; private static string _newLink = @"http://server2.newlink.com/test.txt"; That’s it, then run this console app on the SharePoint server. In the code below, you will notice that we start at the SP Farm level and go through all web applications, site collections and sites (skipping the central admin site).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;

namespace ConsoleApp
{
    class Program
    {
        private static string _originalLink = @"http://server.oldlink.com/test.txt";
        private static string _newLink = @"http://server.newlink.com/test.txt";

        static void Main(string[] args)
        {
            try
            {

                SPFarm farm = SPFarm.Local;
                SPServiceCollection services = farm.Services;

                foreach (SPService service in services)
                {
                    if (service is SPWebService)
                    {
                        SPWebService webService = (SPWebService)service;

                        foreach (SPWebApplication webApp in webService.WebApplications)
                        {
                            if (webApp.IsAdministrationWebApplication == false) // skip central administration
                            {
                                foreach (SPSite siteCol in webApp.Sites)
                                {
                                    using (SPWeb web = siteCol.OpenWeb())
                                    {
                                        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;

                                                            // Check for a specific Content Link in the CEWP and change it if there is a match
                                                            if (contentEditor.ContentLink.Equals(_originalLink))
                                                            {
                                                                contentEditor.ContentLink = _newLink;
                                                            }

                                                            // You can even change the content by using the InnerXml or InnerText property
                                                            // of the Content property. 
                                                            // contentEditor.Content = xmlElement;

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

                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Console.WriteLine("Exception occurred: " + ex.Message);
                                                    }

                                                }

                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred: {0}rn{1}", e.Message, e.StackTrace);
            }
            finally
            {
                Console.Write("Done");
                Console.ReadLine();

            }
        }
    }
}
Author

Steve Borgwardt

Senior Application Developer