以编程方式创建一个包含 webpart 的 webpart 页面 [英] Create a webpart page with an webpart with in it programmatically
问题描述
我想创建多个包含自定义 Web 部件的 Web 部件页面(以编程方式).我已经在 Internetz 上搜索过,但找不到任何可以上班的东西.
I want to create several webpart pages (programmatically) with custom web parts in it. I have searched the internetz but couldn't find anything that I could get to work.
到目前为止,这是我的代码(我在其中创建了一个欢迎页面,而不是 webpart 页面):
Here is my code so far (where I create a welcome page, not a webpart page):
using (SPSite site = new SPSite("http://v99-sp-public/"))
{
using (SPWeb web = site.OpenWeb())
{
PublishingSite pSite = new PublishingSite(site);
SPContentType ctype = pSite.ContentTypes["Welcome Page"];
PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
PageLayout pageLayout = pageLayouts["/_catalogs/masterpage/welcomesplash.aspx"];
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pPages = pWeb.GetPublishingPages();
PublishingPage pPage = pPages.Add("Klanten2.aspx", pageLayout);
SPListItem newpage = pPage.ListItem;
newpage["Title"] = "Klanten";
newpage.Update();
newpage.File.CheckIn("Checkin");
newpage.File.Publish("Publisch");
}
}
请帮帮我,
谢谢.
推荐答案
您可以使用 SPLimitedWebPartManager
和您的 webPart 定义 URL 添加 webpart.这个例子应该让你开始:
You can add webparts by using the SPLimitedWebPartManager
and your webPart defininition URL. This example should get you started:
XmlTextReader reader = new XmlTextReader(new StringReader(web.GetFileAsString(<Url to your .webpart file here>)));
SPLimitedWebPartManager wpm = web.GetLimitedWebPartManager(<URL to your page>, Syste.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
WebPart wp = (WebPart) wpm.ImportWebPart(reader, out errMsg);
wp.Title = "My Title for this webpart";
wpm.AddWebPart(wp, <Name of WebpartZone here, e.g. "Header">, <Zone Index here>);
wpm.SaveChanges(wp);
在您填写空白后,此代码将在您的发布页面上放置一个 WebPart.最后,最重要的函数是 SPLimitedWebPartManager.ImportWebPart
和 SPLimitedWebPartManager.AddWebPart
为发布页面实例化 WebPart 管理器.
After you fill in the blanks this code will put a WebPart on your publishing page. In the end the most important functions are SPLimitedWebPartManager.ImportWebPart
and SPLimitedWebPartManager.AddWebPart
as you instantiate the WebPart manager for the publishing page.
这篇关于以编程方式创建一个包含 webpart 的 webpart 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!