[Sharepoint] Adding a field to a list view with code -> don’t use the DefaultView property

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;

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"];

But still it made us loose precious minutes of our time to get to the solution…
Hope it’s useful to you!


Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>