I need to add a new field to a myriad of existing views of a list in a specific position from left (i.e. this new field must be placed after the field 'fulladdress_onlylistdefined', who already resides in all views).
The field i need to add already exists in list columns.
To do so, i suppose the following actions:
-get the web, list, view
-initialize new field
-get all view fields
-iterate through view fields to locate the field after which i need to place my specific field (using field's internal name)
-insert my field, update view and list
-repeat in loop to iterate through views
Script looks like this:
$web = Get-SPWeb http://server
$list = $web.Lists["Requests"]
$spView = $Web.GetViewFromUrl("/Lists/Requests/View.aspx")
$field1 = $list.Fields["MyField"]
$address = $spview.ViewFields['fulladdress_onlylistdefined']
$spview.ViewFields.Add($field1)
$spview.ViewFields.MoveFieldTo($address,'4')
$spview.Update()
But i got two problems:
When trying to access specific element from ViewFields fields collection i got null instead:
$address = $spview.ViewFields['fulladdress_onlylistdefined'] $address variable is blank
$spview.ViewFields.MoveFieldTo
method won't accept a variable $address as a parameter
My doubt was i should iterate through array, not through collection, but i'm unable to create an array:
$array = @(([array]::CreateInstance([string], 0)))
$coll=$spview.ViewFields.ToStringCollection()
for ($i = 0; $i -lt $coll.Count; $i++)
{
$array=+$coll[$i]
}
Cannot convert value "Edit" to type "System.Int32". Error: "Input string was not in a correct format."
So the final questions are:
How to access specific field in
$spview.ViewFields
fields collection? Especially by its name.How to work with
$spview.ViewFields.MoveFieldTo
method to place a field after specific field in a view?What is the best way to achieve my task?
Thanks!
PS To prevent some solutions, this one does not suit me:
$spview.ViewFields.DeleteAll();
$spview.ViewFields.Add($field1)
$spview.ViewFields.Add($field2)
$spview.ViewFields.Add($field3)
etc