Today we faced a strange problem doing a task as simple as adding a field to the default view (“All items”) with code.
The code would run inside a feature activation and it was something like this:
var view = list.DefaultView; view.ViewFields.Add(fieldName); view.Update();
But… even after the update, the viewfields property remained unchanged…. The view reference was pointing to “All Items”, which was correct, all the existing viewfields were retrieved correctly, but we were unable to add one.
The next logical thing to do was to reference view differently. Referencing it by it’s name did the job. So the working code doesn’t change a lot:
var view = list.Views["All Items"]; view.ViewFields.Add(fieldName); view.Update();
But still it made us loose precious minutes of our time to get to the solution…
Hope it’s useful to you!