How to Add Choices to Existing Choice Field Using CSOM in SharePoint 2013 Online

Code Snippet 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Security;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.SharePoint.Client;  
  9.   
  10. namespace CSOMOffice365  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.   
  17.             string userName = "[email protected]";  
  18.   
  19.             Console.WriteLine("Enter your password.");  
  20.             SecureString password = GetPassword();  
  21.   
  22.             // ClienContext - Get the context for the SharePoint Online Site  
  23.             // SharePoint site URL - https://c986.sharepoint.com  
  24.   
  25.             using (var clientContext = new ClientContext("https://c986.sharepoint.com"))  
  26.             {  
  27.   
  28.                 // SharePoint Online Credentials  
  29.                 clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
  30.   
  31.                 // Get the SharePoint web  
  32.                 Web web = clientContext.Web;  
  33.   
  34.                 // Get the list by Title  
  35.                 List list = web.Lists.GetByTitle("Custom");  
  36.   
  37.                 // Get a specific field by Title  
  38.                 Field field = list.Fields.GetByTitle("Choice");  
  39.                 FieldChoice fieldChoice = clientContext.CastTo<FieldChoice>(field);  
  40.                 clientContext.Load(fieldChoice);  
  41.   
  42.                 // Execute the query to the server  
  43.                 clientContext.ExecuteQuery();  
  44.   
  45.                 // Add the choice field values  
  46.                 List<string> options = new List<string>(fieldChoice.Choices);  
  47.                 options.Add("Option1");  
  48.                 options.Add("Option2");  
  49.                 options.Add("Option3");  
  50.                 fieldChoice.Choices = options.ToArray();  
  51.   
  52.                 // Update the choice field  
  53.                 fieldChoice.Update();  
  54.   
  55.                 // Execute the query to the server  
  56.                 clientContext.ExecuteQuery();  
  57.   
  58.                 // Display all the choices  
  59.                 foreach (string option in fieldChoice.Choices)  
  60.                 {  
  61.                     Console.WriteLine(option);  
  62.                 }  
  63.                 Console.ReadLine();  
  64.             }  
  65.   
  66.         }  
  67.   
  68.         private static SecureString GetPassword()  
  69.         {  
  70.             ConsoleKeyInfo info;  
  71.   
  72.             //Get the user's password as a SecureString  
  73.             SecureString securePassword = new SecureString();  
  74.             do  
  75.             {  
  76.                 info = Console.ReadKey(true);  
  77.                 if (info.Key != ConsoleKey.Enter)  
  78.                 {  
  79.                     securePassword.AppendChar(info.KeyChar);  
  80.                 }  
  81.             }  
  82.             while (info.Key != ConsoleKey.Enter);  
  83.             return securePassword;  
  84.         }  
  85.     }  
  86. }   
Output
 
 
Ebook Download
View all
Learn
View all