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);
        }
上一篇:Asp.net-读取App.config


下一篇:【C语言常见报错----表达式必须是可修改的左值】