How to reorder fields / columns in a SharePoint view?

I add a new field to the list and view. To add a field to the view, I use this code:

view.ViewFields.Add("My New Field"); 

However, this simply ties him to the end of the show. How to add a field to a specific column or reorder fields? view.ViewFields is an SPViewFieldCollection object that inherits from SPBaseCollection, and there are no Insert / Reverse / Sort / RemoveAt methods available.

+4
source share
4 answers

I found removing all the elements from the list and reading them in the order in which I would like to work well (albeit a bit radically). Here is the code I'm using:

 string[] fieldNames = new string[] { "Title", "My New Field", "Modified", "Created" }; SPViewFieldCollection viewFields = view.ViewFields; viewFields.DeleteAll(); foreach (string fieldName in fieldNames) { viewFields.Add(fieldName); } view.Update(); 
+3
source

You can use the default method:

  int newFieldOrderIndex = 1; SPViewFieldCollection viewFields = view.ViewFields; viewFields.MoveFieldTo(fieldName, newFieldOrderIndex); view.Update(); 

https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.spviewfieldcollection.movefieldto.aspx

+1
source

You must use the following method to reorder fields

  string reorderMethod = @"<?xml version=""1.0"" encoding=""UTF-8""?> <Method ID=""0,REORDERFIELDS""> <SetList Scope=""Request"">{0}</SetList> <SetVar Name=""Cmd"">REORDERFIELDS</SetVar> <SetVar Name=""ReorderedFields"">{1}</SetVar> <SetVar Name=""owshiddenversion"">{2}</SetVar> </Method>"; 
0
source

I had two different lists and a similar view. I wanted to update the order of the fields in the mailing list if the user changed the order in the original view.

 ViewFieldCollection srcViewFields = srcView.ViewFields; ViewFieldCollection destViewFields = destView.ViewFields; var srcArray = srcViewFields.ToArray<string>(); var destArray = destViewFields.ToArray<string>(); foreach (var item in destArray) { destViewFields.MoveFieldTo(item, Array.IndexOf(srcArray, item)); destView.Update(); } 
0
source

Source: https://habr.com/ru/post/1277113/


All Articles