Change the fields order in the SharePoint View programmatically

I was asked today how to change the order of the fields programmatically in the SharePoint view (SPView). Since there is no “position” or “order” property, and since the “ViewFields” property returns a “SPViewFieldCollection”, which is not much more than a StringCollection, it’s not that straight-forward.

The easiest way is to accomplish this is to empty the ViewFields property of the current View, sort the fields as you would like to have it, and add it again, one by one, to the ViewFields.

The following code snippet moves the field “Test” to the first position in all the Views except the “Explorer View”.

//iterate all the views
int viewsCount = yourLibrary.Views.Count;
for (int i = 0; i < viewsCount; i++ )
SPView view = yourLibrary.Views[i];

if (view.Title != "Explorer View")

StringCollection oldFields =

//clear all fields

//add wanted field

//add orher fields
foreach (string fieldName in oldFields)
if (fieldName != "test")
if (yourLibrary.Fields.ContainsField(fieldName))