How to Change the Field Type for a List Column Using CSOM in SharePoint 2013 Online
In this blog you will see how to change the field type for a list column using CSOM in SharePoint 2013 Online.
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Security;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
-
- namespace CSOMOffice365
- {
- class Program
- {
- static void Main(string[] args)
- {
- string userName = "[email protected]";
-
- Console.WriteLine("Enter your password.");
- SecureString password = GetPassword();
-
-
-
-
- using (var clientContext = new ClientContext("https://c986.sharepoint.com"))
- {
-
-
- clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
-
-
- Web web = clientContext.Web;
-
-
- List list = web.Lists.GetByTitle("Custom");
-
-
- Field field = list.Fields.GetByTitle("TestCol");
-
-
- field.TypeAsString = "Note";
- field.Update();
- clientContext.Load(field);
-
-
- clientContext.ExecuteQuery();
-
-
- Console.WriteLine(field.Title + " -- Field Type updated to : " + field.TypeAsString);
- Console.ReadLine();
-
- }
-
- }
-
- private static SecureString GetPassword()
- {
- ConsoleKeyInfo info;
-
-
- SecureString securePassword = new SecureString();
- do
- {
- info = Console.ReadKey(true);
- if (info.Key != ConsoleKey.Enter)
- {
- securePassword.AppendChar(info.KeyChar);
- }
- }
- while (info.Key != ConsoleKey.Enter);
- return securePassword;
- }
- }
- }
Output