Set Choice Field Allow 'Fill-in' Choice option As TRUE For all the Lists in Site Collection Programmatically using C# CSOM in SharePoint Online

Here I demonstrate how to update Choice column property AllowFillInChoice as True for all the Lists in Site Collection using console application










using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
namespace AllowFillInChoice
{
    class Program
    {
        static void Main(string[] args)
        {

            using (var ctx = new ClientContext("https://@@@@@@@.sharepoint.com/sites/Dev/"))
            {
                string userName = "---------@-------.onmicrosoft.com";
                string pwd = "--------";
                var securePassword = new SecureString(); ;
                foreach (char c in pwd)
                {
                    securePassword.AppendChar(c);

                }
                securePassword.MakeReadOnly();
                try
                {
                    ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);
                    Site site = ctx.Site;
                    Web web = ctx.Web;
                    ctx.Load(web);
                    ctx.ExecuteQuery();
                    ListCollection listcoll = web.Lists;
                    ctx.Load(listcoll);
                    ctx.ExecuteQuery();
                    foreach (List olist in listcoll)
                    {
                        if (olist.BaseTemplate == 100)
                        {
                            List list = web.Lists.GetByTitle(olist.Title);
                            ctx.Load(list);
                            ctx.Load(list.Fields);
                            ctx.ExecuteQuery();
                            foreach (var field in list.Fields)
                            {
                                if (field.TypeAsString == "Choice")
                                {

                                    System.Console.WriteLine(field.Title + " - " + field.TypeAsString);
                                    var choiceField = ctx.CastTo<FieldChoice>(field);
                                    choiceField.FillInChoice = true;
                                    choiceField.Update();
                                    ctx.Load(field);
                                    ctx.ExecuteQuery();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }
            }
        }

    }
}

Comments

Popular posts from this blog

Download Visakhapatnam Tirupati AC Double Decker Express Act