Adding a field or Column to a SharePoint list using CSOM SharePoint Online

Adding a field or Column to a SharePoint list using CSOM SharePoint Online

In this article you will learn to Adding a field or Column to a SharePoint list using CSOM SharePoint Online.Using the Add(Field) method or AddFieldAsXml(String, Boolean, AddFieldOptions) method of the FieldCollection class we can add a field in SharePoint List. Below code creates a field in SharePoint List.

using Microsoft.SharePoint.Client;
using System;
using System.Security;
using System.Linq;

namespace TokenAuthentication
{
    class Program
    {
       static string Passwords = "Password";
       static string UserName = "UserName";
       static string SiteURL = "Site URL";
        static void Main(string[] args)
        {

            using (ClientContext clientCont = new ClientContext(SiteURL))//PWA Url
            {
                SecureString passWord2 = new SecureString();
                foreach (char c in Passwords.ToCharArray()) passWord2.AppendChar(c);
                clientCont.Credentials = new SharePointOnlineCredentials(UserName, passWord2);
                List oList = clientCont.Web.Lists.GetByTitle("City");

                Field oField = oList.Fields.AddFieldAsXml("<Field DisplayName='New Field' Type='Number' />",

                true, AddFieldOptions.DefaultValue);

                FieldNumber fieldNumber = clientCont.CastTo<FieldNumber>(oField);
                fieldNumber.MaximumValue = 100;
                fieldNumber.MinimumValue = 35;

                fieldNumber.Update();

                clientCont.ExecuteQuery();
            }
        }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.