Cannot insert the value NULL into column Name, Thanks SharePoint

March 7, 2008

My feature code was throwing an error today. For those who don’t know what I’m talking about here is a crash course: A feature basically allows you to attach functionality to something in SharePoint. Maybe you want to add a menu option to the ‘Site Actions’ menu, or import some files into libraries. In my case I wanted to attach functionality to a specific list. I wrote my feature code, and it seemed fine. Deployed and activated just great.

Then I tried to create a Custom List. I had event receivers attached to custom lists (ListTemplateID=100 for those who care) and i got this:

Cannot insert the value NULL into column ‘Name’, table ‘[somesharepointcontentdatabase].dbo.EventReceivers’;
column does not allow nulls. INSERT fails.
The statement has been terminated.

Took me a good two hours of trying everything I could think of with my XML and code, only to be repeatedly thwarted by this database-level error! I tried commenting out different sections of my receivers and still got the error. When I commented the entire <Receivers> section, things ran fine, but not the <Receiver> items inside. What the hell is going on? Read on!

I’ll give an example of what I’m talking about here – the following comes from MSDN:

feature.xml:
<Feature
Scope=”Web”
Title=”Simple Updating Item Event Handler Registration”
Id=”A6B8687A-3200-4b01-AD76-09E8D163FB9A”
xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>
<ElementManifest Location=”elements.xml”/>
</ElementManifests>
</Feature>

inside the feature.xml file (above) it references the following file:

elements.xml:

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Receivers
ListTemplateOwner=”ADDABAAA-1111-2222-3333-111111111111″
ListTemplateId=”104″>
<Receiver>
<Name>SimpleUpdateEvent</Name>
<Type>ItemUpdating</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>SimpleUpdateEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=10b23036c9b36d6d</Assembly>
<Class>MS.Samples.SimpleItemUpdateHandler</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>

This works fine. Here’s something interesting though: any comments – <!– Like this> - you have in the <Receivers> node will be literally interpreted as <Receiver> nodes! That means whenever it tries to attach event receivers to any list, it treats the comments as actual receivers! That’s right, for whatever reason, the code that ties event receivers to lists literally loops through every node (comments are still considered nodes!) and tries to use them instead of using any sort of XPath. How hard is it to use “//Elements/Receivers/Receiver” as your node collection XPath? I really hope whoever wrote that code got the ruler or something.

The solution: Remove comments from your Feature xml file(s).

I say this because God only knows where else this is happening, and the odds of you realizing that it is something that should just work, like comments, are probably not that high. Remember folks, sometimes the most obtuse problems have the most simple answers. Try not to over-think things.

A huge thank you must go out to Janne Mattila for being the first (and apparently only according to Google) documenting this. I wish I had looked sooner.

Categories: Bad bad bad Bonus Code Microsoft Technology

Tagged under: , , , , , ,

Comments, Disussion and so forth