Fix for List Event Receiver not working in SharePoint Online

August 20th, 2012 | Posted by Jeffrey Paarhuis in Developing

I created a List Event Receiver for my custom List Definition and wanted to set a view list properties programmatically in the EventReceiver. It worked perfectly in SharePoint 2010 but not in SharePoint Online.

To get the list I used the SPListEventProperties.List, but this object was causing some issues in SharePoint Online. I solved it by using the SPListEventProperties.Web.Lists[listname].

Code before:

SPList list = properties.List;

//enable content types
list.ContentTypesEnabled = true;

list.Update();

Code after:

SPList list = properties.List;
 SPWeb web = properties.Web;

// Re-opening the list, seems to be necessary because SharePoint Online has some sort of bug or timing issue.
 list = web.Lists[list.Title];

// enable content types
 list.ContentTypesEnabled = true;

list.Update();

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

%d bloggers like this: