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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- namespace CreateColumns {
- class Program {
- static void Main(string[] args) {
- ClientContext clientcontext = new ClientContext("Add your site here");
- Web web = clientcontext.Web;
- List list = web.Lists.GetByTitle("My List");
- string columnxml = "<Field DisplayName='MyCustomColumn' Type='Text' />";
- list.Fields.AddFieldAsXml(columnxml, true, AddFieldOptions.DefaultValue);
- clientcontext.ExecuteQuery();
- Console.WriteLine("Column Created");
- Console.ReadKey();
- }
- }
- }
Run the code and your custom column will be created.