2

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:

  1. When trying to access specific element from ViewFields fields collection i got null instead:

    $address = $spview.ViewFields['fulladdress_onlylistdefined']  
    $address variable is blank
    
  2. $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
1

Finally, I've finished with creating console application.

I'm not great in C# so it is a bit bulky but this solution works.

If there is any mistakes, i will be glad to hear about it.

var reader = new StreamReader(File.OpenRead(@"C:\temp\views.csv"));
            List<string> listA = new List<string>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');
            listA.Add(values[0]);
        }
        var data = listA.ToArray();
        SPSite oSiteCollection = new SPSite("http://server");
        using (SPWeb oWebsite = oSiteCollection.RootWeb)
        {
            SPList oList = oWebsite.Lists["Requests"];
            foreach (string i in data)
            {
                SPView oView = oWebsite.GetViewFromUrl(i.Substring(19));
                SPViewFieldCollection collViewFields = oView.ViewFields;
                System.Collections.Specialized.StringCollection collStrings = oView.ViewFields.ToStringCollection();
                var z = collStrings.IndexOf("fulladdress_onlylistdefined");
                collViewFields.Add("RequestVoltageTotal");
                oView.Update();
                Console.WriteLine("Field added: RequestVoltageTotal");
                collViewFields.MoveFieldTo("RequestVoltageTotal", z + 1);
                oView.Update();
            }

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.