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. namespace CSOMOffice365
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. string userName = "[email protected]";
  16. Console.WriteLine("Enter your password.");
  17. SecureString password = GetPassword();
  18. // ClienContext - Get the context for the SharePoint Online Site
  19. // SharePoint site URL - https://c986.sharepoint.com
  20. using (var clientContext = new ClientContext("https://c986.sharepoint.com"))
  21. {
  22. // SharePoint Online Credentials
  23. clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
  24. // Get the SharePoint web
  25. Web web = clientContext.Web;
  26. // Get the list by Title
  27. List list = web.Lists.GetByTitle("Custom");
  28. // Get a specific field by Title
  29. Field field = list.Fields.GetByTitle("Choice");
  30. FieldChoice fieldChoice = clientContext.CastTo<FieldChoice>(field);
  31. clientContext.Load(fieldChoice);
  32. // Execute the query to the server
  33. clientContext.ExecuteQuery();
  34. // Add the choice field values
  35. List<string> options = new List<string>(fieldChoice.Choices);
  36. options.Add("Option1");
  37. options.Add("Option2");
  38. options.Add("Option3");
  39. fieldChoice.Choices = options.ToArray();
  40. // Update the choice field
  41. fieldChoice.Update();
  42. // Execute the query to the server
  43. clientContext.ExecuteQuery();
  44. // Display all the choices
  45. foreach (string option in fieldChoice.Choices)
  46. {
  47. Console.WriteLine(option);
  48. }
  49. Console.ReadLine();
  50. }
  51. }
  52. private static SecureString GetPassword()
  53. {
  54. ConsoleKeyInfo info;
  55. //Get the user's password as a SecureString
  56. SecureString securePassword = new SecureString();
  57. do
  58. {
  59. info = Console.ReadKey(true);
  60. if (info.Key != ConsoleKey.Enter)
  61. {
  62. securePassword.AppendChar(info.KeyChar);
  63. }
  64. }
  65. while (info.Key != ConsoleKey.Enter);
  66. return securePassword;
  67. }
  68. }
  69. }
Output