Delete field (column) from a SharePoint list Using C#(CSOM)

string strUrl = ConfigurationManager.AppSettings["SiteUrl"];
string strWebUrl = ConfigurationManager.AppSettings["WebUrl"];
string strListName = ConfigurationManager.AppSettings["ListName"];
string strFieldName = ConfigurationManager.AppSettings["FieldName"];

            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                using (SPSite site = new SPSite(strUrl))
                {
                    using (SPWeb web = site.OpenWeb(strWebUrl))
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList list = web.Lists[strListName];
                        if (list != null)
                        {
                            foreach (SPField field in list.Fields)
                            {  
                                if (field.Title == strFieldName)
                                {
                                    field.AllowDeletion = true;
                                    field.Delete();
                                    list.Update();
                                    break;
                                }
                            }
                        }
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });

            Console.WriteLine("Success");

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
幸福是一件礼物,得到它的秘诀是不怀期待,只在它来的时候尽情享受
原文地址:https://www.cnblogs.com/selenazhou/p/14389014.html