Back Incorrect SPView usage

RESP510247

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.

Incorrect usage:

public void IncorrectSPViewUpdate(SPList list)
{
	// won't save view (!!!)
	list.Views["TestView1"].DefaultView = true;
	list.Views["TestView1"].Update();

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

}

Correct usage:

public void CorrectSPViewUpdate(SPList list)
{
	// save SPView instance into a local varible, always!
	var view = list.Views["TestView1"];

	view.DefaultView = true;
	view.Paged = true;

	view.Update();
}

Links

Last updated: 1/9/2016
Comments
Login to leave a comment.