开发者

SharePoint - Set custom publishing page layout custom field programatically

开发者 https://www.devze.com 2023-01-17 08:50 出处:网络
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 pag

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:

Pu开发者_运维百科blishingPage 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. 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. So adding the content type first solved my problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消