How To Create A Column In SharePoint 2013 Programmatically

Welcome to a blog on how to create a column in a SharePoint 2013 list programmatically, using a Console Application. We will use Visual Studio to create a column in the list.

Let’s see, how to do it.
  • Open your Visual Studio.
  • Select New Project.

     
  • Select Console Application.
  • Add the references Microsoft.SharePoint dll and Microsoft.SharePoint.Client dll.
  • Paste the code, given below, under Program.cs.
Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. namespace CreateColumns {  
  8.     class Program {  
  9.         static void Main(string[] args) {  
  10.             ClientContext clientcontext = new ClientContext("Add your site here");  
  11.             Web web = clientcontext.Web;  
  12.             List list = web.Lists.GetByTitle("My List");  
  13.             string columnxml = "<Field DisplayName='MyCustomColumn' Type='Text' />";  
  14.             list.Fields.AddFieldAsXml(columnxml, true, AddFieldOptions.DefaultValue);  
  15.             clientcontext.ExecuteQuery();  
  16.             Console.WriteLine("Column Created");  
  17.             Console.ReadKey();  
  18.         }  
  19.     }  
  20. }  
Run the code and your custom column will be created.