SharePoint Config

Ari Bakker's thoughts on customising and configuring SharePoint

Error creating SharePoint publishing pages

without comments

This post describes how to use the ContentTypeBinding element to avoid an error that can occur creating publishing pages in the following scenario:

  • Custom content types and associated page layouts are installed on a Publishing Site.
  • A user with ‘contribute’ rights creates a page based on a content type that does not exist in the pages library for the current site.

What happens in this situation is that the publishing features try to automatically copy the content type from the root site into the pages library. If the user only has ‘contribute’ permissions (e.g. is in the default Site Members group) this fails with the following error message:

Object reference not set to an instance of an object.   at Microsoft.SharePoint.Publishing.PublishingPage.SetContentType… (full details below)

When searching for this error I found a couple of posts that suggested solutions using C# code but an easy way to fix this is to bind the custom content types to the pages library so that they already exist when users try to create new pages, thus avoiding the error. This can be accomplished through the use of the ContentTypeBinding  element in a web scoped feature. This also allows the content type to show up on the ‘new’ menu. An example feature to do this is shown below:

Feature Xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<Feature  Id=”bb3b9420-e97f-11de-8a39-0800200c9a66″
          Title=”Custom content type bindings”
          Description=”Binds custom page content types to the pages library”
    <ElementManifest Location=”elements.xml”/>   

Elements Xml:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=””>

  <!—Custom Article Page –>
  <ContentTypeBinding ContentTypeId=”0x010100C568DB52D9D0A14D9B2FDCC966…” ListUrl=”Pages” />


The key things to note here is the ContentTypeBinding element that has a ContentTypeId and ListUrl attributes. So adding in bindings for all your custom page content types provides a clean way of preventing the error and getting your content types to appear on the new menu of the pages library. This approach can also be used to bind content types to lists that have been defined outside of the current feature scope (to bind a content type to a custom list definition you are creating use the ContentTypeRef element as described in the post Creating a Custom SharePoint List Definition)

The other thing to note is that the feature is scoped to Web (as I haven’t got this to work at other levels but your mileage may vary).

The screenshots below show how this change is reflected in the UI.



After (note additional content type available):


The full error message is:

Object reference not set to an instance of an object.   at Microsoft.SharePoint.Publishing.PublishingPage.SetContentType(SPContentType listContentType)
   at Microsoft.SharePoint.Publishing.PublishingPageCollection.<>c__DisplayClass5.<Add>b__0()
   at Microsoft.Office.Server.Diagnostics.FirstChanceHandler.ExceptionFilter(Boolean fRethrowException, TryBlock tryBlock, FilterBlock filter, CatchBlock catchBlock, FinallyBlock finallyBlock)
   at Microsoft.Office.Server.Diagnostics.ULS.SendWatsonOnExceptionTag(ULSTagID tagID, ULSCat categoryID, String output, Boolean fRethrowException, TryBlock tryBlock, CatchBlock catchBlock, FinallyBlock finallyBlock)
   at Microsoft.SharePoint.Publishing.PublishingPageCollection.Add(String name, PageLayout layout)
   at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.NewPageItemSave(String pageName, PageLayout pageLayout)
   at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.CreateStandardPage(String pageName)
   at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.ButtonCreatePage_Click(Object sender, EventArgs e)
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Publishing Page creation bug on Robin Meure’s blog

PublishingPage create error on Bernd’s blog

ContentTypeBinding Element on MSDN

Post to Twitter Post to Delicious Post to Digg Post to Reddit Post to StumbleUpon

Written by Ari Bakker

December 15th, 2009 at 3:26 pm

Leave a Reply