开发者

Is it possible to add Bamboo Web Parts to a web part page programmatically in SharePoint?

Can't find much information on how to开发者_开发问答 do this at all - how can I add in a web part and then configure the settings for it?


Here's a code snippet that will do that for you. In this example, I put a Content Editor Web Part on the page and set the content of it programmatically. If you want to find out what properties your web part has, you can manually put it on a page and export it. Examine the exported file for the property names. In your case, must must reference the 3rd party DLL, and use the name on the desired web part instead of the ContentEditorWebPart. You can find out the name by either using the Object Browser or Reflector.

SPFile spPageFile = web.GetFile(targetFilePath);
using (SPLimitedWebPartManager theMan = spPageFile.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
  ContentEditorWebPart cewp = new ContentEditorWebPart();
  cewp.ChromeType = PartChromeType.None;
  XmlDocument xmlDoc = new XmlDocument();
  XmlElement xmlEl = xmlDoc.CreateElement("NewCEWP");
  xmlEl.InnerText = string.Format(@"<h2>Blah blah blah...</h2>");

  cewp.Content = xmlEl;
  theMan.AddWebPart(cewp, "Main", 0);
  theMan.SaveChanges(wp);
}

Hope this helps.


  1. Load the page you want to add the web part to via object model.
  2. Get the SPLimitedWebPartManager for this page.
  3. Add the web part you want via the AddWebPart() method.
  4. If your web part uses the normal web part configuration then you can access the settings via the web parts properties.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜