22
Dec
11

How To modify the DefaultView of a List

 

If you want to modify the DefaultView of a programmatically created list you have to know a “bagatelle”.

First i tried it this way:

list.DefaultView.ViewFields.Add(list.fields.GetFieldByInternalName(“internalName”);

list.DefaultView.Update();

and nothing happened at all.

The reason lies in the property DefaultView which encapsulates a behavior in which in every call the View is instantiated again. So in the first line you would modifiy the first instance of the view. Than, you would update a new instance of the View, which is not modified or updated.

So, knowing this, what you have to do to make it working is the following:

Get the Instance, modifiy it, and safe it.

SPView view = list.DefaultView;

if(view.ViewFields.Exists("internalName")==false)
   view.ViewFields.Add(list.Fields.GetFieldByInternalName("internalName"));
view.Update();

December 2011
M T W T F S S
« Jun    
 1234
567891011
12131415161718
19202122232425
262728293031  

Badge Farm

  • Powered by Redoable 1.0



0 Responses to “How To modify the DefaultView of a List”


  1. No Comments

Leave a Reply