SharePoint: Add site column to list programmatically using CSOM

This SharePoint CSOM tutorial, we will discuss how to add site columns to existing SharePoint Online lists or document libraries using csom (.Net managed object model code).

Recently I got one requirement to add one site column to a few lists and document libraries using C#.Net (Microsoft.SharePoint.Client.dll) code.

Add site column to list programmatically using CSOM in SharePoint

Here I have created a console application and written the below method which will take a single parameter as URL (need to provide the site URL).

Here the listNames contains all my lists. As per my requirement, I have added the same site column to 3 different lists.

private static IList<string> listNames = new List<string>();

public static void AddSiteColumnToExistingList(string URL)
{
listNames.Add("List1");
listNames.Add("List2");
listNames.Add("List3");

using (ClientContext ctx = new ClientContext(URL))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
foreach (string targetListName in listNames)
{
List targetList = ctx.Web.Lists.GetByTitle(targetListName);
ctx.Load(targetList);
ctx.ExecuteQuery();
string siteCols = "MySiteColumnName";
FieldCollection fieldCollection = ctx.Web.AvailableFields;
ctx.Load(fieldCollection);
ctx.ExecuteQuery();
Field myField = Enumerable.FirstOrDefault(fieldCollection, ft => ft.InternalName == siteCols);
targetList.Fields.Add(myField);
targetList.Update();
ctx.ExecuteQuery();
}
}
}

private static SecureString GetSPOSecureStringPassword()
        {
            try
            {
                var secureString = new SecureString();
                foreach (char c in "MyPassword")
                {
                    secureString.AppendChar(c);
                }
                return secureString;
            }
            catch
            {
                throw;
            }
        }

        private static string GetSPOAccountName()
        {
            try
            {
                return "bijay@<tenantname@onmicrosoft.com>";
            }
            catch
            {
                throw;
            }
        }

Once you will run the code, it will add the “MySiteColumnName” site column to all the 3 SharePoint Online lists.

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we learned how to add site column to SharePoint list programmatically using CSOM.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SPGuides.com

>