CSC510247. Multiple SPView instances could not be updated at once.

Description

SPList.DefaultView and SPList.Views[] properties return a new SPView instance with every call. To handle a single instance you need to retrieve the SPView object and modify it directly.

Bad code:
list.Views["TestView1"].DefaultView = true;
list.Views["TestView1"].Update();

list.DefaultView.ViewFields.Add("NewField1");
list.DefaultView.ViewFields.Add("NewField2");
list.DefaultView.Update(); // saves the "NewField2" only!

Good code:
SPView view = list.Views["TestView1"];
view.DefaultView = true;
view.Paged = true;
view.Update();

Links

SPView.Update method

Last edited Jun 2, 2014 at 9:03 PM by dvd73, version 4

Comments

No comments yet.