locked
Set custom publishing page layout custom field programatically RRS feed

  • Question

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

     

    I have also tried:

    newPublishingPage.ListItem.ContentType.Fields["PageContentCategory"]

     

    and also:

    newPublishingPage.ContentType.Fields["PageContentCategory"]

     

    None of this works. Is there any way for me to set the custom field value from code like this?

    Thanks.

    Tuesday, September 28, 2010 8:20 PM

Answers

  • In above code you are adding column to the page item that is in library but not to content type.

    I think your flow is wrong, Here i will show you step by step process to create new page from custom content type

    1: Create custom content type by inheriting any default content type and save it.

    2: Create page layout from this custom content type.

    3: Create publishing page from new created custom page layout.

    In brief

    //Create content type first
    //Suppose contenttypeID is the ’0×01010…’ string ID of my content type

    //Create page layout from content type
    //Suppose pageLayoutName is name ‘MyPageLayout.aspx’ of my custom page layout

    //Now create page
    PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
    SPContentTypeId cTypeId = new SPContentTypeId(contenttypeID);
    
    PageLayout[] layouts = pWeb.GetAvailablePageLayouts(cTypeId);
    //PageLayout layout = layouts[1];
    foreach (PageLayout p in layouts)
    {
    	if (p.Name == pageLayoutName)
    	{
    		layout = p;
    		break;
    	}
    }
    PublishingPage newPage = pWeb.GetPublishingPages().Add("MyCustomPage.aspx", layout);
    newPage.Update();
    
    


    Regards, Sandip Patil, .Net,Sharepoint Developer www.worldofsharepoint.com
    • Marked as answer by KeFang Chen Wednesday, October 6, 2010 9:48 AM
    Wednesday, September 29, 2010 4:29 AM

All replies

  • In above code you are adding column to the page item that is in library but not to content type.

    I think your flow is wrong, Here i will show you step by step process to create new page from custom content type

    1: Create custom content type by inheriting any default content type and save it.

    2: Create page layout from this custom content type.

    3: Create publishing page from new created custom page layout.

    In brief

    //Create content type first
    //Suppose contenttypeID is the ’0×01010…’ string ID of my content type

    //Create page layout from content type
    //Suppose pageLayoutName is name ‘MyPageLayout.aspx’ of my custom page layout

    //Now create page
    PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
    SPContentTypeId cTypeId = new SPContentTypeId(contenttypeID);
    
    PageLayout[] layouts = pWeb.GetAvailablePageLayouts(cTypeId);
    //PageLayout layout = layouts[1];
    foreach (PageLayout p in layouts)
    {
    	if (p.Name == pageLayoutName)
    	{
    		layout = p;
    		break;
    	}
    }
    PublishingPage newPage = pWeb.GetPublishingPages().Add("MyCustomPage.aspx", layout);
    newPage.Update();
    
    


    Regards, Sandip Patil, .Net,Sharepoint Developer www.worldofsharepoint.com
    • Marked as answer by KeFang Chen Wednesday, October 6, 2010 9:48 AM
    Wednesday, September 29, 2010 4:29 AM
  • Here is a more complete code snippet:

    private PageLayout[] GetAvailablePageLayouts(bool onlyGetArticlePageLayouts)
    {
    	if (onlyGetArticlePageLayouts)
    		return this.currentPublishingWeb.GetAvailablePageLayouts(articleContentType.Id);
    	else
    		return this.currentPublishingWeb.GetAvailablePageLayouts();
    }
    
    private void HandleCreateNewArticlePage()
    {
    	string pageContentCategory = Request.Params["pageContentCategory"];
    	string pageContent = Request.Params["pageContent"];
    	string pageDescription = Request.Params["pageDescription"];
    	string pageLayoutFileName = Request.Params["pageLayout"];
    	string pageName = Request.Params["pageName"];
    	string pageTitle = Request.Params["pageTitle"];
    
    	PageLayout newPageSelectedLayout = GetAvailablePageLayouts(true).Where(x => x.Name == pageLayoutFileName).FirstOrDefault();
    
    	try
    	{
    		this.CurrentSite.AllowUnsafeUpdates = true;
    		this.CurrentWeb.AllowUnsafeUpdates = true;
    
    		PublishingPage newPublishingPage = this.currentPublishingWeb.GetPublishingPages().Add(pageName, newPageSelectedLayout);
    		newPublishingPage.Update();
    
    		newPublishingPage.ListItem[new Guid("{93496B35-7EC3-4132-B0D0-3BDC5606F5EF}")] = pageContentCategory; //Fails
    		newPublishingPage.ListItem["PageContentCategory"] = pageContentCategory; //Fails
    		newPublishingPage.ListItem.ContentType.Fields["PageContentCategory"] //Fails
    		newPublishingPage.ContentType.Fields["PageContentCategory"] //Fails
    		newPublishingPage.ListItem[FieldId.PublishingPageContent] = pageContent;
    		newPublishingPage.Title = pageTitle;
    		newPublishingPage.Update();
    		newPublishingPage.CheckIn("");
    		newPublishingPage.ListItem.File.Publish("");
    
    		if (newPublishingPage.ListItem.ModerationInformation != null)
    		{
    			newPublishingPage.ListItem.File.Approve("");
    		}
    
    		this.CurrentSite.AllowUnsafeUpdates = false;
    		this.CurrentWeb.AllowUnsafeUpdates = false;
    
    		this.currentPublishingWeb.Update();
    	}
    	catch (Exception ex)
    	{
    		HandleServerError(ex);
    		return;
    	}
    }
    

     

    I do have a custom content type that inherits from the "Page" content type. And I have several page layouts based on this content type. I can create a new page fine as long as I don't try to set the value of my custom field. But if I try to set the field value in any of the 4 ways shown above, I get an exception thrown. So my problem isn't creating the page, it's setting my custom field value.

     

    Any ideas?

    • Proposed as answer by Florin Duca Monday, October 4, 2010 3:22 PM
    Wednesday, September 29, 2010 7:09 PM
  • Make sure that the page library has the content type. It is not necessary the case (internally, it looks like SharePoint is adding the content type to the page library the first time you create/update a page to use that content type).

     

    To check if the pages library has the content type already and to add it if not already there :

    SPList list = newPublishingPage.ListItem.ListItems.List;

    if (list.ContentTypes["My Content Type"] == null)

    {

    list.ContentTypes.Add(rootWeb.ContentTypes["My Content Type"]); // here the rootWeb is the root web of the site collection

    }

    SPContentType ct = list.ContentTypes["My Content Type"];

     

    Next, to set the content type for an existing page, just set the property :

    newPublishingPage.ListItem["ows_ContentTypeId"] = my content type Id for the current Page library;

    Haven’t tried it but it should work to use the Layout with the Add method. 

    Also, make sure that you take the content type ID for your content type on the Pages library, it will not be the same as your site content type, it will actually be a child content type (a guid added to the site content type ID).


    Florin DUCA Logica Management Consulting, France
    • Proposed as answer by Florin Duca Monday, October 4, 2010 3:25 PM
    Monday, October 4, 2010 3:23 PM