[英]Create a page programatically with C# in SharePoint 2007
Not sure if this is asked, but searching hasn't quite yielded what I'm looking for. 不知道是否要问这个问题,但是搜索并没有完全满足我的要求。 I have a page layout already, what I need to do is programmatically create a page in the Pages library.
我已经有了页面布局,我需要做的是在Pages库中以编程方式创建一个页面。
I'm fuzzy on the details, but somehow I think I will need to open the Layout, then stream it to a page and then save the page. 我对细节不了解,但是我想我将需要打开Layout,然后将其流式传输到页面,然后保存页面。 I'm unsure how to go about this.
我不确定该怎么做。
The page is context sensitive so I think I'll begin with using SPSite and SPWeb to get access to the lists. 该页面是上下文相关的,因此我认为我将首先使用SPSite和SPWeb来访问列表。
What I'm unclear on is, how can I get the Layouts? 我不清楚的是,如何获得版式? I think I should be able to add a page somewhat like this:
我认为我应该能够添加如下页面:
SPWeb web = SPContext.Current.Site.OpenWeb();
SPList Pages = web.Lists["Pages"];
SPListItemCollection splc = Pages.Items;
foreach (SPListItem spli in splc)
{
if (spli.Name == "lmIntraTopicsArticle")
{
}
}
SPListItem sli = splc.Add();
Pages.Update();
SPFolder PagesFolder = Pages.RootFolder;
byte[] layoutContents = new byte[20];
SPFile myNewPage = PagesFolder.Files.Add(PagesFolder.Url + "/TopicWindowArchive.aspx", layoutContents);
web.Update();
Now I need to figure out how to add content from a layout. 现在,我需要弄清楚如何从布局中添加内容。 Update in a few if I figure it out.
如果我弄清楚了,请更新一些。
Thank you, 谢谢,
The trick is to get a PublishingWeb object. 诀窍是获取一个PublishingWeb对象。 That contains the layouts.
包含布局。
See here for an example 参见这里的例子
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
string pageName = “MyCustomPage.aspx”;
PageLayout[] pageLayouts = publishingWeb.GetAvailablePageLayouts();
PageLayout currPageLayout = pageLayouts[0];
PublishingPageCollection pages = publishingWeb.GetPublishingPages();
PublishingPage newPage = pages.Add(pageName,currPageLayout);
newPage.ListItem[FieldId.PublishingPageContent] = “This is my content”;
newPage.ListItem.Update();
newPage.Update();
newPage.CheckIn(“This is just a comment”);
Also check this answer 还要检查这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.