Showing posts with label SPListItemEventReceiver. Show all posts
Showing posts with label SPListItemEventReceiver. Show all posts

Wednesday, April 20, 2011

Title column value in ItemUpdating event for Document Library

I had to add some logic in ItemUpdating event receiver when anything was changed except one particular column. This had to work for both lists and libraries.
There some good posts about differences in properties for lists and libraries, e.g. this one from Randy Williams.

My code was

string[] ignoreFields = new string[] { "myField" };
foreach (SPField field in properties.ListItem.Fields)
{
if (!ignoreFields.Contains(field.InternalName) &&
properties.AfterProperties[field.InternalName] != null &&
(properties.ListItem[field.Id] == null ||
properties.ListItem[field.Id].ToString() != properties.AfterProperties[field.InternalName].ToString()))
{
// do something
}
}


This worked well for lists but I struggled with Title column in libraries.
I found properties.AfterProperties["Title"] is always null for libraries even when Title was changed.

After intensive googling I found that for libraries properties.ListItem.Title is mapped to properties.AfterProperties["vti_title"].

So, I had to add this

if (properties.List is SPDocumentLibrary && properties.ListItem.Title != properties.AfterProperties["vti_title"])
{
// do something
}

Thursday, October 21, 2010

DisableEventFiring is obsolete in SharePoint 2010

You can check this in msdn. However DisableEventFiring()/EnableEventFiring() still works as per Reflector.
Recommended way now is to use new property EventFiringEnabled. This gives us an option to check current status and save it. I found a great post from Adrian Henke and modified his code using this new feature.



The class DisabledItemEventsScope disable/enable event firing for the current thread and could be used in that manner at any place including code behind for the page or web part



BTW: There are couple useful SPList extensions to manage SPListItemEventReceiver collection