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