Random Technical Thoughts

April 9, 2011

How to add a content editor web part to a SharePoint page

Filed under: SharePoint, Web Part — Tags: , — chrisbarba @ 10:40 pm

Here is some code to add a content editor web part to a page.
So if you are creating a page in code (like during feature activation), then you put a content editor web part on the page.

Setting the InnerText (contentXMLElement.InnerText = "";) will set the text in the content editor, incase you want something to be there by default.  User HTML.

 

using (SPWeb webSite = SPContext.Current.Site.OpenWeb(SiteToOpen))
{
     using (SPLimitedWebPartManager mgr = webSite.GetFile("default.aspx").GetLimitedWebPartManager(PersonalizationScope.Shared))
     {
          if (mgr != null)
          {
               # region AddNewLink
 
               ContentEditorWebPart cewp = new ContentEditorWebPart();
               cewp.AllowClose = false;
               cewp.AllowEdit = false;
               cewp.AllowHide = false;
               cewp.AllowMinimize = false;
               cewp.ID = "ContentEditorWP";
               cewp.Title = "Content Editor Web Part";
               cewp.ChromeType = PartChromeType.None;
 
               //Add content to the content editor web part
               XmlDocument addNewXMLDoc = new XmlDocument();
               XmlElement contentXMLElement = addNewXMLDoc.CreateElement("Root");
               contentXMLElement.InnerText = "";
               cewp.Content = contentXMLElement;
               cewp.Content.InnerText = contentXMLElement.InnerText;
 
                    
               // add the web part.   
               // first argument: web part object   
               // second argument: zone   
               // third argument: index (location within the zone)   
               mgr.AddWebPart(cewp,"left", 0);
 
               # endregion
 
          }
     }
}        

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

About these ads

1 Comment »

  1. It had been a while since I visited web site with such high quality information. Thansk rather a lot for the useful info. I see your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work Look forward to reading more from you in the future.

    Comment by Wilson Palakiko — April 10, 2011 @ 10:44 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: