Null reference exception when adding content type to document library at Microsoft.SharePoint.SPFile.UpdateInternal

I was adding content types to a document library using C# and got a Null reference exception when calling SPList.ContentTypes.Add(contentType). The stack was:

at Microsoft.SharePoint.SPFile.UpdateInternal(Boolean migrationSemantics, Boolean reGhost)
   at Microsoft.SharePoint.SPContentTypeCollection.AddContentType(SPContentType contentType, Boolean updateResourceFileProperty, Boolean checkName, Boolean setNextChildByte)
   at Microsoft.SharePoint.SPContentTypeCollection.Add(SPContentType contentType)
   at Harness.BernadoHarness.Execute() in C:\Project\BernadoHarness.cs:line 24
   at Harness.Program.Main(String[] args) in C:\Project\Program.cs:line 34
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

My code to add the content type was fairly innocent:

using (var site = new SPSite("http://myServer/subSite1"))
{
	using (var web = site.OpenWeb())
	{
		SPList list = web.Lists["Documents Library"];
		SPContentType contentType = null;

		contentType = web.AvailableContentTypes["Meeting Agenda"];
		list.ContentTypes.Add(contentType);
	}
}

It turns out that it was because I have 2 ItemEventReceivers (ItemUpdating and ItemAdded) on the document library and these were raised when the content type was being added to the document library. When these events are raised as part of the content type being added, the properties.ListItem would be null. My 2 receivers do some work with the properties.ListItem object, but did not anticipate that this could be null, hence they felt over and raised a Null exception to the SPList.ContentTypes.Add() method. Adding a null check to my 2 receivers fixed the issue.

Now, the interesting point here is why adding a content type to the library would raise the ItemUpdating and ItemAdded events? This is because adding a content type to a document library may involve adding/updating the document template file to the resource folder of the content type, which is a sub-folder of the library – and hence raise the item events.

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in SharePoint 2010. Bookmark the permalink.

One Response to Null reference exception when adding content type to document library at Microsoft.SharePoint.SPFile.UpdateInternal

  1. KjellSJ says:

    Thank you, saved me a lot of debugging.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s