Recently I had a strange problem creating a publishing page through the object model in SharePoint 2007. Trying to create the page with a user that seemed to have all necessary rights failed with this exception:
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) …
Second time trying to create the page with the same user in the same pages library worked. Creating the page through the UI worked on first try.
Looking at the error message we wondered about the SetContentType statement. Obviously the pages list, where I tried to create the page, did not have the content type that the page was using.
Either giving the user list administration rights or adding the content type to the pages library solved the issue.
This looks like a nice Microsoft bug:
- Page creation should fail each time.
- Page creation should fail from both UI and object model.
- A nice error message could be displayed…