The circle of life – ** .Net – Training – MOSS **

  • Past Post

  • Vistor Locations

  • Advertisements

Add Web Part to page using code

Posted by Clayton James on December 31, 2008

     // add Content Editor Web Part
     SPFile NewPage = Web.GetFile(NewPageUrl);
     SPLimitedWebPartManager mgr;
     mgr = NewPage.GetLimitedWebPartManager(PersonalizationScope.Shared);

     ContentEditorWebPart wp1 = new ContentEditorWebPart();
     wp1.Title = txtPageTitle.Text;
     wp1.ChromeType = PartChromeType.TitleOnly;
     wp1.AllowClose = false;
     XmlDocument doc = new XmlDocument();
     string ns1 = “http://schemas.microsoft.com/WebPart/v2/ContentEditor”;
     XmlElement elm = doc.CreateElement(“Content”, ns1);
     elm.InnerText = txtPageContent.Text;
     wp1.Content = elm;

     // add Web Part to Left Zone
     mgr.AddWebPart(wp1, “Left”, 0);


One Response to “Add Web Part to page using code”

  1. Thanks a lot!This is very useful!

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 )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: