I am trying to add a new column called "REF" to all existing views using powershell which I have succeeded in doing, however when adding the column it is automatically added to the end of the array making it last in the view, I need it to be at the beggining [0]

Can I add the column in at point [0] forcing the other columns to move down the array possibly like this?


Or would I need to copy out the array and re add it in the new order?

$originalArray = $view.ViewFields    

What is the best approach?




You do the following to reorder fields in wild lists:

  • Create an array of the fields order (Or spreadsheet/xml file I use spreadsheets)
  • Delete the viewfields with SPView.ViewFields.DeleteAll()
  • Loop through the array for the order you want (or spreadsheet which is what I use) and add the field in question SPView.ViewFields.Add(FieldName);
  • SPView.Update()
  • library/list.Update()

I use this as a reusable script, and I find it is the best approach (Similar to what you have already done)

  • Rather than using XML / spreadsheet can I get the code to save the current order and then readd them, I'm not sure what type of object to use to achieve this? I thought the below but it doesnt seem to work... $originalViewFields1 = New-Object System.Collections.ArrayList $originalViewFields = $view.ViewFields – Colin Cook Nov 19 '12 at 16:33
  • SPViewFieldCollection, is basically a StringCollection. So it should be "New-Object System.Collections.Specialized.StringCollection" – Hugh Wood Nov 19 '12 at 16:46

Thanks to Hugh Wood answerm, I could made this quick and dirty solution.

You can copy and past you wanted order in $order (even from excel sheet) and so get simply the requested order. Please be aware there is no logical check:

Add-PsSnapin Microsoft.SharePoint.PowerShell
$web = get-spweb https://sharepoint/sites/site
$list = $web.lists["yourListName"]
$view = $list.views["Main View"]
$orginalViewFields = $view.ViewFields
$order.Split([Environment]::NewLine) | where { -not [string]::IsNullOrEmpty($_) } | % 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.