Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > Publishing Page creation bug
July 30
Publishing Page creation bug

Overview

This post describes an issue with creating pages in a publishing environment using the UI.

 

Cause

Given the following scenario :

  • RootWeb with some PageLayouts.
  • Several subsites with unique permissions
    • User A has contribute permissions on a subsite and has read/limited access on the Portal

User A tries to create a new page based on a Page Layout which is a Site ContentType and not yet ‘installed’ on the Pages Library. During creation the user gets the following error:

 

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) …

 

If the user tries to create the page again (by pressing F5 in the error screen), the page is successfully created.

If the PageLayout is already installed on the Pages Library there is no error at all and the page is created immediately. 


So it seems that the error is raised when SharePoint tries to add the ContentType of the PageLayout during the creation of the page. I’ve tried to mimic these behavior using a console application with the following code :

static void Main(string[] args)
{
    using (SPSite site = new SPSite("http://test/a/c/d/e"))
    {   
        using (SPWeb web = site.OpenWeb())
        {
            SPContentType cType = web.Site.RootWeb.ContentTypes["Link"];
            SPList list = web.Lists["Shared Documents"];             
            SetContentType(cType, list);
        }
    }
}
public static void SetContentType(SPContentType contentType, SPList list)
{
    list.ContentTypes.Add(contentType);
    list.Update();
}

The code breaks on the .Add of the ContentType, prompting me with the following error “Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))”  but when executing the code again I get the following message “A duplicate name "Contact" was found.” Indicating the ContentType is installed the first time when it threw the Access Denied exception. That confirms that when an user tries to create a page based on a PageLayout that was not already installed on the Pages Library the first time, the PageLayout is correctly installed the second time and thus the page get’s created.

 

When I change the permissions of User A to Full Control I get no error at all and everything works smoothly.

 

So what actually is happening, is that the PublishingPageCollection.Add method tries to add the ContentType using the current context and does not check if the user has sufficient permissions to add the ContentType to the Pages Library.

 

Googling on the subject gave me this blogpost by Bernd who also came across this weird behavior..  

 

Solution

 

Preventive  

A solution can be to add all the (relevant) PageLayouts to each Pages Library using a featurereceiver and/or a stapler to prevent the action (the add action of the ContentType by SharePoint during the creation of the Page) from happening.

 

Reactive  

A console application / timerjob / page with longoperation method that loops through every publishingWeb and adds the PageLayouts from the publishingSite to the Pages Library.

Comments

There are no comments for this post.
 

 Statistics

 
Views: 2164
Comments: 0
Tags:
Published:1023 Days Ago