Export (0) Print
Expand All

SPFieldCollection.Delete method

Deletes the field with the specified internal, display or static name from the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public void Delete(
	string strName
)

Parameters

strName
Type: System.String

A string that specifies the internal, display, or staticname or title of the field. SharePoint Foundation first checks the specified value by internal name and then by title.

ExceptionCondition
SPException

The field is read-only.

ArgumentException

No field by that name exists in the collection.

InvalidOperationException

The value returned by the field's CanBeDeleted property is false.

This method gets the name of the field from a field property in the following order: InternalName, Title, StaticName.

If the field is of type SPFieldLookup and it is a primary lookup field, then all secondary lookups are also deleted. For more information, see the IsDependentLookup property.

The following code example deletes the field with the specified name from the Events list in all the subsites under a specified site.

SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs["MySite"].Webs;
foreach (SPWeb oWebsite in collWebsites)
{
    SPFieldCollection collFields = oWebsite.Lists["Announcements"].Fields;
    collFields.Delete(collFields["MyField"].InternalName);
}
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Show:
© 2014 Microsoft