Categories
Developing

Fix for List Event Receiver not working in SharePoint Online

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:

[code language=”csharp”]

SPList list = properties.List;

//enable content types
list.ContentTypesEnabled = true;

list.Update();

[/code]

Code after:

[code language=”csharp”]

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();

[/code]