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

发布于 2024-09-24 16:32:29 字数 1178 浏览 5 评论 0原文

我有一个基于发布文章页面内容类型的自定义发布页面内容类型。在此内容类型上,我有一个名为“PageContentCategory”的自定义字段。在我创建新页面的代码中,我尝试了以下操作:

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();

我还尝试通过字段名称来设置它:

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();

这两种方法都会引发错误。有什么方法可以让我在这样的代码中设置自定义字段的值吗?

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". 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-10-01 16:32:29

尝试在 newPublishingPage.Listitem 上调用 Update 方法,而不是在 newPublishingPage 本身上调用。
像这样:

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

然后你可能还需要其中一些行,具体取决于页面库的配置

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

Try calling the Update method on newPublishingPage.Listitem not on newPublishingPage itself.
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();
红墙和绿瓦 2024-10-01 16:32:29

因此,我的问题的解决方案是,我必须以编程方式将内容类型添加到页面列表中,而不是在第一次添加具有该内容类型的页面时自动添加它。显然,如果您让 SharePoint 自动将内容类型添加到页面列表,那么它不会以某种方式正确绑定。所以首先添加内容类型解决了我的问题。

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. So adding the content type first solved my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文