SPFieldCollection.Item property (String)

Gets the field with the specified display name from the collection. In Microsoft C#, this property is an indexer for the SPFieldCollection class.

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

Syntax

'Declaration
Public ReadOnly Default Property Item ( _
    displayName As String _
) As SPField
    Get
'Usage
Dim instance As SPFieldCollection
Dim displayName As String
Dim value As SPField

value = instance(displayName)
public SPField this[
    string displayName
] { get; }

Parameters

  • displayName
    Type: System.String

    A string that contains the display name.

Property value

Type: Microsoft.SharePoint.SPField
A SPField object that represents the field.

Examples

The following code example uses an indexer to delete a field from a specified list.

This example requires using directives (Imports in Microsoft Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

Dim webSite As SPWeb = SPContext.Current.Site.AllWebs("MySite")
Try
    Dim fields As SPFieldCollection = webSite.Lists("MyList").Fields
    Dim delField As String = fields("MyField").Title

    fields.Delete(delField)
Finally
    webSite.Dispose()
End Try
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["MySite"])
{
    SPFieldCollection collFields = oWebsite.Lists["MyList"].Fields;
    string strDelField = collFields["MyField"].Title;

    collFields.Delete(strDelField);
}

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

Item overload

Microsoft.SharePoint namespace