Wednesday, October 22, 2008

How to programatically delete a SharePoint 2007 field belonging to a list or document library?

Here's the code for deleting a field named "phone" in list "myList" on a site with URL http://mysite/:


try {

using (SPSite sitecollection = new SPSite("http://mysite/")) {

using (SPWeb web = sitecollection.OpenWeb()) {

web.AllowUnsafeUpdates = true;

// Delete Sharepoint SPField

web.Lists["myList"].Fields.Delete("phone");

web.Update();

Console.WriteLine("Done deleting column");

}

}

} catch (Exception ex) {

Console.WriteLine(ex.ToString());

}

No comments:

Post a Comment