SPField Class

Represents a field in a list on a Windows SharePoint Services Web site.

Inheritance Hierarchy


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


<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public Class SPField

Dim instance As SPField
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPField


Use the Fields property of either the SPList class or the SPListItem class to return an SPFieldCollection object that represents the collection of fields for a list or list item. Use an indexer to return a single field from this collection. For example, if the collection is assigned to a variable named collFields , use collFields[index] in C#, or collFields(index) in Visual Basic, where index is either the index number of the field in the collection or the display name of the field.

If you set properties of the SPField class and its inheriting classes, you must call the Update for changes to take effect in the database.


The following code example iterates through all the lists in all the subsites under a site and, if it finds a list with a specified name, updates the title, default value, and description for a field.

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

Dim siteCollection As SPSite = SPContext.Current.Site
Dim sites As SPWebCollection = siteCollection.AllWebs("Site_Name").Webs
Dim site As SPWeb

For Each site In  sites

    Dim lists As SPListCollection = site.Lists
    Dim i As Integer

    For i = 0 To lists.Count - 1

        If lists(i).Title = "List_Name" Then

            Dim fieldChange As SPField = lists(i).Fields("Field_Name")

            fieldChange.DefaultValue = "Default_Value"
            fieldChange.Description = "Description"
            fieldChange.Title = "New_Field_Title"


        End If

    Next i

Next site 
SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
    SPFieldCollection collFields = oWebsite.Lists["List_Name"].Fields;
    for (int intIndex = 0; intIndex < collFields.Count; intIndex++)
        string[] strRefFields = collFields[intIndex].FieldReferences;

        if (strRefFields != null)
            for (int intStrPos = 0; intStrPos < strRefFields.Length; 
                Label1.Text += collFields[intIndex].InternalName +
                " :: " + SPEncode.HtmlEncode(strRefFields[intStrPos]) 
                + "<BR>";


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 Best Practices: Using Disposable Windows SharePoint Services Objects.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also


SPField Members

Microsoft.SharePoint Namespace

Inheritance Hierarchy