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


Sathish Nadarajan
SharePoint MVP
Published On :   04 Dec 2017
Visit Count
Today :  3    Total :   379
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics