简体   繁体   English

SharePoint-以编程方式设置自定义发布页面布局自定义字段

[英]SharePoint - Set custom publishing page layout custom field programatically

I have a custom publishing page content type, based on the Publishing Article Page content type. 我有一个基于发布文章页面内容类型的自定义发布页面内容类型。 On this content type, I have a custom field named "PageContentCategory". 在此内容类型上,我有一个名为“ PageContentCategory”的自定义字段。 In my code to create new pages, I tried this: 在创建新页面的代码中,我尝试了以下操作:

PublishingPage newPublishingPage = this.currentPublishingWeb.GetPublishingPages().Add(pageName, newPageSelectedLayout);

if (pageContent.IsEmpty())
{
 pageContent = Properties.Resources.EAWorldArticleHandler_CreateNewArticlePage_DefaultPageContent;
}

newPublishingPage.ListItem[new Guid("{93496B35-7EC3-4132-B0D0-3BDC5606F5EF}")] = pageContentCategory;
newPublishingPage.ListItem[FieldId.PublishingPageContent] = pageContent;
newPublishingPage.Title = pageTitle;
newPublishingPage.Update();

I have also tried to set it by the field name: 我也尝试通过字段名称设置它:

PublishingPage newPublishingPage = this.currentPublishingWeb.GetPublishingPages().Add(pageName, newPageSelectedLayout);

if (pageContent.IsEmpty())
{
 pageContent = Properties.Resources.EAWorldArticleHandler_CreateNewArticlePage_DefaultPageContent;
}

newPublishingPage.ListItem["PageContentCategory"] = pageContentCategory;
newPublishingPage.ListItem[FieldId.PublishingPageContent] = pageContent;
newPublishingPage.Title = pageTitle;
newPublishingPage.Update();

Both of these methods throw an error. 这两种方法都会引发错误。 Is there any way for me to set my custom field's value in code like this? 有什么办法可以在这样的代码中设置自定义字段的值?

Try calling the Update method on newPublishingPage.Listitem not on newPublishingPage itself. 尝试在newPublishingPage.Listitem而不是newPublishingPage本身上调用Update方法。 Like this: 像这样:

newPublishingPage.ListItem["PageContentCategory"] = pageContentCategory;
newPublishingPage.ListItem.Update();

and then you maybe also need some of these lines, depending in the configuration of your page library 然后您可能还需要其中一些行,具体取决于页面库的配置

newPublishingPage.Checkin();
newPublishingPage.Publish();
newPublishingPage.Approve();

So, the solution to my problem was that I had to programmatically add the content type to the pages list instead of letting it be added automatically the first time a page with that content type was added. 因此,解决我的问题的方法是,我必须以编程方式将内容类型添加到页面列表中,而不是在第一次添加具有该内容类型的页面时自动将其添加。 Apparently if you let SharePoint automatically add the content type to the pages list then it somehow doesn't get bound properly. 显然,如果让SharePoint自动将内容类型添加到页面列表中,那么它将以某种方式无法正确绑定。 So adding the content type first solved my problem. 因此,添加内容类型首先解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM