views:

403

answers:

2

We have a sharepoint website that has been quite heavily developed with content using the out of the box content editor webpart. This is a bit rubbish when it comes to using any other browser than IE.

We have in mind to update to the free Telerik content editor, however we would like to save ourselves a large amount of copy and pasting, and clicking and selecting to swap the web parts over.

There seems to be no official upgrade path but it seems to me that it must be possible to use the power of code (tm) to grab the content of the original webpart, new up a telerik webopart and put the content into the new webpart... then delete the old original web part.

Has anyone done this sort of thing? How is it best to lay out the code and actually run the code that will do the swap (if it is possible). A fresh webpart with a "Do Swap" button on? or something more sophisticated?

I also would need to walk through every page and subsite (and page in subsite) and look at every web part. Is there a best practice way of doing this?

Thanks and sorry for being a bit rambley!

+1  A: 

I'm not 100% certain on this, but if your content is saved into a list as individual fields, could you not point the Telerik controls at where that content is saved? This may cause Telerik controls to freak out a bit on the first edit of that content, but they should be ok to re-format the content and recover.

Joe Swan
+2  A: 

I would write a console application for this.

Open the root Web and iterate through its sub webs. Do the work needed for each page, by opening the WebPartManager.

Here are some pseudo code to get you started.

string SourceUrl = "http://ThisWeb";

using (SPSite sourceSite = new SPSite(SourceURL))
{
    using (SPWeb sourceWeb = sourceSite.OpenWeb(SourceURL))
    {
     IterateSubWebsProd(sourceWeb):
    }
}

private void IterateSubWebsProd(SPWeb sourceWeb)
{
    // This is the migration function
    DoThingyWithThisWeb(sourceWeb);

    foreach (SPWeb subWeb in sourceWeb.Webs)
    {
      IterateSubWebsProd(subWeb);
      subWeb.Dispose();
    }
}

private void DoThingyWithThisWeb(SPWeb sourceWeb)
{
    PublishingPage currentPage = null;

    string currentPageName = "<something>";
    // Find the pages that you want to modify, with a CAML query for example
    SPQuery query = new SPQuery();
    query.Query = string.Format("" +
    "<Where>" +
      "<Eq>" +
      "<FieldRef Name='FileLeafRef' />" +
      "<Value Type='File'>{0}</Value>" +
      "</Eq>" +
    "</Where>" +
    "", currentPageName);


     // This codesnippet is from a Publishing web example
     PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(sourceWeb);
    PublishingPageCollection pageColl = publishingWeb.GetPublishingPages(query);
    if (pageColl.Count > 0)
    {
     currentPage = pageColl[0];
    }

    using (SPLimitedWebPartManager wpMan = currentPage.ListItem.File.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
    {
     foreach (WebPart wp in wpMan.WebParts)
     {
      if (wp.GetType().Equals(typeof(Microsoft.SharePoint.WebPartPages.ContentEditorWebPart)))
      {
       Microsoft.SharePoint.WebPartPages.ContentEditorWebPart thisWebPart = wp as Microsoft.SharePoint.WebPartPages.ContentEditorWebPart;

       // This is just dummy code, here you will do your content migration 
       XmlDocument xmlDoc = new XmlDocument();
       XmlElement xmlElement = xmlDoc.CreateElement("RootElement");
       xmlElement.InnerText = sourceItem[SourceField].ToString();
       thisWebPart.Content = xmlElement;

       wpMan.SaveChanges(thisWebPart);
      }
     }
    }
}
Magnus Johansson