Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

$view.ViewFields[0].Add($refField)

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

$originalArray = $view.ViewFields    
$view.ViewFields.Add($refField)
$view.ViewFields.Add($originalArray[0])
$view.ViewFields.Add($originalArray[1])
$view.ViewFields.Add($originalArray[2])

What is the best approach?

Thanks,

Colin.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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)

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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