Essentially what I want is to pre-load the existing content editor with some text may be from a configuration(by adminstrator) or from the list. So I am creating a custom wp that will act like a wrapper to the existing content editor and add some text to it. But this makes the CEWP uneditable to the user. Please suggest. I am using SharePoint 2010 foundation.
2 Answers
Here's my working code for SP 2010 for adding text to the existing CEWP. I think it must be identical to the example mentioned above:
SPListItem pageItem;// Need to get page SPListItem
SPLimitedWebPartManager manager = SPContext.Current.Web.GetLimitedWebPartManager(pageItem.Url, PersonalizationScope.Shared);
ContentEditorWebPart contentEditor = manager.WebParts.OfType<ContentEditorWebPart>().Single(); // You can choose a specific CEWP instance by any conditions if you like
XmlDocument xDoc = new XmlDocument();
var contentRoot = xDoc.CreateElement("ContentRoot");
string content = "abcde!";
contentRoot.InnerText = editor.Content;
contentEditor.Content = contentRoot;
manager.SaveChanges(contentEditor);
Wrapping the webpart with custom code shouldn't be needed; should be able to do what you want by updating the contents. The following article should get you moving in the right direction.
Programmatically Updating a SharePoint Content Editor Web Part -- note this is for SharePoint 2007, but as you didn't mention what version you are using...
-
-
It is a great solution but what if I had more than one content editor on the page? I am looking more towards setting (preloading) text to a specific CEWP on the page and still keep it editable for the user.– Hna0002Nov 7, 2011 at 19:36