与它的编程Web部件创建一个web部件页 [英] 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.
下面是我到目前为止的代码(如我创建了一个欢迎页面,而不是一个web部件页):
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
和你的WebPart defininition URL。这个例子应该让你开始:
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.
这篇关于与它的编程Web部件创建一个web部件页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!