How to Add/Create Columns To the SharePoint List Using CSOM

Create Column in the SharePoint List Using CSOM

Please follow the steps to execute the code in visual studio using Console Application




Steps: 
  1. Open your Visual Studio.
  2. From the template select Console Application and click next.
  3. Enter name and select the location to store the project and click create.
  4. Now your program. cs file will open, write/copy, paste the below-given code.

 

using Microsoft.SharePoint.Client;
using System;

namespace CSOMCRUD 
 { 
  class Program
   { 
try
     {
       using (ClientContext clientContext = new ClientContext("Site URL"))
{
           Web web = clientContext.Web;
   
   // Add the List Name in which you want to add the column
           List list = web.Lists.GetByTitle("Employee");
   
   // Add field as XML which contains Name, Display Name , Type and  Isrequired fields
           list.Fields.AddFieldAsXml(@"<Field 
                                           Name='Expiry Date'
                                           DisplayName='Expiry Date'
                                           Type='DateTime'
                                           Format='DateOnly'
                                           Required='FALSE'
                                           >
                                        <Default>[today]</Default>
                                      </Field>", true, AddFieldOptions.DefaultValue);
  
// Execute the query   
            clientContext.ExecuteQuery();
        }
                
      catch (Exception ex)
        {
           throw;
        }
    }
 }


 5. Click F5 or Start in Visual Studio, to add the column to the list.



Thank You for reading. Hope you have enjoyed this! :-)

Comments