Welcome to Atlanta .NET Regular Guys Sign in | Join | Help

How to programmatically add a field to all views in a SharePoint list

Today I had a need to add a new field to multiple lists each with multiple views and to group by the new column in each of the views.  There were about 6 lists and 12 or so views on each list and doing this through the interface, although possible, would have been tedious, error prone and very boring (especially for the client since he was doing it).  So I decided to automate the process.  I added the field to each of the lists manually, although I could have automated that too :).  I then wrote a simple console application that accepts a few parameters, namely the new fields internal name, the site collection URL, the web path, the list or library name, the position of the field, whether or not to group by that field, and whether or not to collapse the groups (if grouped).

Here is the main function.  I have a few helper functions that gather input but this is the meat an potatoes.

 

static void Main(string[] args)
{
  // if all input is valid then proceed
  if (true == GatherInput())
  {
    // get a reference to the site collection
    using (SPSite site = new SPSite(siteUrl))
    {
      // get a reference to the site
      using (SPWeb web = site.OpenWeb(webPath))
      {
        // get a reference to the list or library
        SPList list = web.Lists[listLibName];

        // iterate though all the views in a list
        for (int i = 0; i <= list.Views.Count - 1; i++)
        {
          // get a reference to a view
          SPView view = list.Views[i];

          // only do this for visible views and ignore the useless explorer view :)
          if (view.Hidden == false && !(view.Title == "Explorer View"))
          {
            // retrieve the names of all of the fields used in the view
            StringCollection viewFields = view.ViewFields.ToStringCollection();

            // remove all of the fields from the view
            view.ViewFields.DeleteAll();

            // create the new field to insert
            SPField newField = new SPField(list.Fields, newFieldName);

            // add each of the fields back into the view
            // while adding the new field at the desired location
            for (int k = 0; k < viewFields.Count; k++)
            {
              SPField field = new SPField(list.Fields, viewFields[k]);

              if (k == Convert.ToInt32(fieldOrdinal))
              {
                view.ViewFields.Add(newField);
              }
              view.ViewFields.Add(field);
            }

            if (true == groupByField)
            {
              // group by the new column, collapsing the group if necessary
              string query = string.Empty;
              query += "<GroupBy Collapse=\""
              if (true == collapseGroup)
              {
                query += "TRUE"
              }
              else
              {
                query += "FALSE"
              }
              query += "\">"
              query += "<FieldRef Name=\"" + newField.InternalName + "\" />"
              query += "</GroupBy>"
              view.Query += query;
            }

            // update the view
            view.Update();
          }
        }
      } 
      site.RootWeb.Dispose(); //this was opened implicitly when we referenced SPSite and needs to be disposed.
    }
  }
}

Published Wednesday, March 28, 2007 6:34 PM by Dan Attis
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: How to programmatically add a field to all views in a SharePoint list

Hey Dan, how about writing this as a windows application and incorporating it in my utility pack? check it out- http://www.codeplex.com/spstipsUtilityPack
Wednesday, March 28, 2007 8:46 PM by Ishai Sagi

# WSS FAQ - additions and corrections XXXXVIII - 26th March - 1st April 2007

Saturday, March 31, 2007 11:56 PM by Mike Walsh's WSS and more

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:47 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:47 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:48 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:48 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:48 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:49 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:49 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:49 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:50 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:50 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:51 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:52 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:53 PM by Mosterboy

# Mosterboy

<a href="http://lipstick.com/user/ArizonaDiamondbacks/">Arizona">http://lipstick.com/user/ArizonaDiamondbacks/">Arizona Diamondbacks Tickets</a>
[url= http://lipstick.com/user/ArizonaDiamondbacks/ ]Arizona Diamondbacks Tickets[/url]
http://lipstick.com/user/ArizonaDiamondbacks/ Arizona Diamondbacks Tickets
Sunday, August 19, 2007 4:53 PM by Mosterboy

# buy cheap viagra

Tuesday, October 16, 2007 6:47 PM by buy cheap viagra

# buy viagra

<a href="http://cheapestviagra.forum66.com/">buy cheap viagra</a> buy cheap viagra,<a href="http://www.geocities.com/buyviagrahere/buy_viagra.html">buy viagra online</a> buy viagra online,
Sunday, November 11, 2007 6:21 AM by buy viagra

# buy viagra

<a href="http://www.geocities.com/buyviagrahere/buy_viagra.html">buy viagra</a> buy viagra,<a href="http://www.geocities.com/buyviagrahere/buy_viagra.html">buy viagra online</a> buy viagra online,
Sunday, November 11, 2007 8:28 AM by buy viagra

# re: How to programmatically add a field to all views in a SharePoint list

Tuesday, November 13, 2007 3:24 AM by GIOCHI

# re: How to programmatically add a field to all views in a SharePoint list

Tuesday, November 13, 2007 5:33 PM by acyclovir

# re: How to programmatically add a field to all views in a SharePoint list

[<a href=google.con>I love google</a> I love google I love google I love google I love google
Friday, January 18, 2008 10:05 PM by hello

# re: How to programmatically add a field to all views in a SharePoint list

Saturday, January 19, 2008 9:22 AM by Danijer

# re: How to programmatically add a field to all views in a SharePoint list

Hello, [URL]http://kapis.servetown.com/viagra-prescription.html[/URL]
<a href = "http://kapis.servetown.com/buy-online-viagra.html "> book buy online order viagra</a>
Saturday, January 19, 2008 11:32 AM by Hikary

# re: How to programmatically add a field to all views in a SharePoint list

Saturday, January 19, 2008 12:43 PM by Danijer

# re: How to programmatically add a field to all views in a SharePoint list

Saturday, January 19, 2008 4:46 PM by Kolonqw

# re: How to programmatically add a field to all views in a SharePoint list

Hello, <a href = "http://zalup.servetown.com/buying-viagra.html "> buying viagra</a>
<a href = "http://zalup.servetown.com/payday-cash-advance.html "> cash payday loan advance bzhtml</a>
Saturday, January 19, 2008 8:53 PM by Britnek

# re: How to programmatically add a field to all views in a SharePoint list

Saturday, January 19, 2008 10:35 PM by Danijer

# re: How to programmatically add a field to all views in a SharePoint list

I love your site
Monday, January 28, 2008 3:56 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

I love your site
Monday, January 28, 2008 4:01 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

I love your site:) here is mine 00usa.net
Monday, January 28, 2008 5:58 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

I love your site:) here is mine http://00usa.net
Monday, January 28, 2008 5:59 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

I love your site:) here is mine 00usa.net
Monday, January 28, 2008 6:02 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

I love your site:) here is mine http://00usa.net
Monday, January 28, 2008 6:04 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

I love your site:) here is mine 00usa.net
Monday, January 28, 2008 6:09 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

I love your site:) here is mine http://00usa.net
Monday, January 28, 2008 6:11 PM by Saddam

# re: How to programmatically add a field to all views in a SharePoint list

Saturday, February 16, 2008 4:17 PM by Arnold

# re: How to programmatically add a field to all views in a SharePoint list

Saturday, February 16, 2008 4:17 PM by Arnold

# My feedback

<a href= http://cartinageopoliticasiena.helpsaua.info/ >cartina geopolitica siena</a>  
<a href= http://kisahartismalaysia.helpsaua.i
Saturday, May 31, 2008 4:47 AM by Bill

# My feedback

<a href= http://airavs.envy.nu/sissynobbylyricstodaletter.html >sissy nobby lyrics to da letter</a> [URL= http://airavs.envy.nu/sissynobbylyricstodaletter.html ]sissy nobby lyrics to
Monday, June 30, 2008 12:22 AM by Hero

# non extradition countries


- However, with a certain open-mindedness, let also say for those who without awakening follow a by rote prescription of feeling warm fuzzies because they engage the teachings from the side of a rational man or woman, and that discovery can transform every part of their rhetoric, have no new maps of Consciousness to revelate or relate.  [url=http://glohret.seitenclique.net/kayne-la02/lexington-ky-breaking-news.html] lexington ky breaking news [/url] [url=http://glohret.seitenclique.net/kayne-la02/britney-spears-images.html] britney spears images [/url]
Monday, September 08, 2008 4:32 AM by accecyfeegope

What do you think?

(required) 
required 
(required)