Share via


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Sub Delete ( _
    strName As String _
)
'Usage
Dim instance As SPFieldCollection
Dim strName As String

instance.Delete(strName)
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.

Exceptions

Exception Condition
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.

Remarks

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.

Examples

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

Dim siteCollection As SPSite = SPContext.Current.Site
Try
    Dim webSites As SPWebCollection = siteCollection.AllWebs("MySite").Webs

    Dim webSite As SPWeb
    For Each webSite In  webSites
        Dim fields As SPFieldCollection = 
            webSite.Lists("Announcements").Fields

        fields.Delete(fields("MyField").InternalName)
    Next webSite
Finally
    siteCollection.Dispose()
End Try
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);
}

Note

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.

See Also

Reference

SPFieldCollection Class

SPFieldCollection Members

Microsoft.SharePoint Namespace