In this article we want to show how to create a field in list
using client object model
We are having a requirement to create a new fields in
SharePoint site which is in different server, so we created a console application
and implemented the code using client object model to create a new fields in
the list as shown below.
This is a sample example to create new fields.
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Newfield
{
class Program
{
static void
Main(string[] args)
{
ClientContext context = new ClientContext(
"http://dotnetsharepoint.com/sites/sharepoint/");
context.Credentials = new NetworkCredential("username",
"password", "domain");
context.Load(context.Web);
List c_list =
context.Web.Lists.GetByTitle("YourListName");
Field
newfields = c_list.Fields.AddFieldAsXml("<Field
DisplayName = 'YourFieldName' Type = 'Text' />",
true, AddFieldOptions .DefaultValue);
FieldText
textfield = context.CastTo<FieldText>(newfields);
textfield.Update();
context.ExecuteQuery();
}
}
}