SharePoint Office 365 – Site Columns – Choice Field AllowFillIn Choice as TRUE – Programmatically C# CSOM

Sathish Nadarajan
 
Solution Architect
December 4, 2017
 
Rate this article
 
Views
2828

I was doing a Migration from SP2010 to SP2016 and met with a strange pre-requisite. The Choice Column got created in the Target but, the default setting is AllowFillInChoice as FALSE. Hence, if there are 5 choices in the Source, the corresponding Choices were not available while creating the column in the Target. Hence, as a prerequisite, we tried updating the Choice column property with a small piece of code, which will iterate through all the target sites and update the choice fields.

The piece of code is straight forward and I hope there is no explanation required for the scenario and the code.

 public static void AllowFillinChoiceASTRUE()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*****.sharepoint.com/sites/developersite";
 
 
             // UserName 
             string userName = "sathish@**********.onmicrosoft.com";
 
             //Password
             string password = "**********";
 
              
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 try
                 {
                     Site site = clientContext.Site;
                     Web web = clientContext.Web;
 
                     clientContext.Load(web);
                     clientContext.Load(web.Lists);
                     clientContext.ExecuteQuery();
 
                     List list = web.GetListByTitle("D1");
                     clientContext.Load(list);
                     clientContext.Load(list.Fields);
                     clientContext.ExecuteQuery();
 
                     foreach (var field in list.Fields)
                     {
                         if (field.TypeAsString == "Choice")
                         {
 
                             System.Console.WriteLine(field.Title + " - " + field.TypeAsString);
                             var choiceField = clientContext.CastTo<FieldChoice>(field);
                             choiceField.FillInChoice = true;
                             choiceField.Update();
                              
 
                             clientContext.Load(field);
                             clientContext.ExecuteQuery();
                         }
                     } 
                 }
                 catch (Exception ex)
                 {
                     System.Console.WriteLine(ex.Message);
                 }
             }
 
             System.Console.WriteLine("Completed....");
             System.Console.WriteLine("Press Any Key to Exit ....");
             System.Console.ReadLine();
         }
     }
 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment