Udostępnij za pośrednictwem


Code snippet to add / modify columns in a list

It iterates through a whole site collection and wherever the list with name “test” it finds, It will do the changes.

    1:  using System;
    2:  using System.Collections.Generic;
    3:  using System.Text;
    4:  using Microsoft.SharePoint;
    5:   
    6:  namespace ConsoleApplication1
    7:  {
    8:      class Program
    9:      {
   10:          static void Main(string[] args)
   11:          {
   12:              using (SPSite site = new SPSite("https://ms9:101"))
   13:              {
   14:                  foreach (SPWeb web in site.AllWebs)
   15:                  {
   16:                      SPList list = web.Lists["test"];
   17:                      SPFieldCollection col = list.Fields;
   18:                      SPField field = col.GetField("Trigger");
   19:                      field.Type = SPFieldType.Choice;
   20:                      field.Update();
   21:                      col.Add("Cost", SPFieldType.Currency, false);
   22:                      
   23:                      col.Add("Due Date", SPFieldType.DateTime, false);
   24:                      list.Update();
   25:                      web.Dispose();
   26:                  }
   27:              }
   28:          }
   29:      }
   30:  }

image