Add default site columns to list using Client Object Model

  • Posted by: Softree Technology

Below code describes how to add site columns from site to SharePoint list.

  1. Add reference “Microsoft.sharepoint.client.dll” and “Microsoft.sharepoint.client.Runtime.dll”.
  2. Write Below Code.

class Program

{

static void Main(string[] args)

{

string listName = “New List”;

using (ClientContext context = new ClientContext(“siteUrl”))

{

context.Credentials = new System.Net.NetworkCredential(“UserName”, “Password”); //Apply Credential

Web web = context.Web;

context.Load(web, w => w.AvailableFields, w => w.Lists);

List destList = web.Lists.GetByTitle(listName);

context.Load(destList);

context.ExecuteQuery();

foreach (Field field in web.AvailableFields)

{

 if (field.Title == “Department”) //Apply condition which field you want to add

destList.Fields.Add(field);

}

}

}

Author: Softree Technology