Share via


SPField Class

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

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPField
    

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

Syntax

<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

Remarks

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.

Examples

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"

            fieldChange.Update()

        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; 
                intStrPos++)
            {
                Label1.Text += collFields[intIndex].InternalName +
                " :: " + SPEncode.HtmlEncode(strRefFields[intStrPos]) 
                + "<BR>";
            }
        }
    }
}

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 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

Reference

SPField Members

Microsoft.SharePoint Namespace

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPField
    Microsoft.SharePoint.SPFieldAttachments
    Microsoft.SharePoint.SPFieldBoolean
    Microsoft.SharePoint.SPFieldCalculated
    Microsoft.SharePoint.SPFieldComputed
    Microsoft.SharePoint.SPFieldCrossProjectLink
    Microsoft.SharePoint.SPFieldDateTime
    Microsoft.SharePoint.SPFieldFile
    Microsoft.SharePoint.SPFieldLookup
    Microsoft.SharePoint.SPFieldMultiChoice
    Microsoft.SharePoint.SPFieldMultiColumn
    Microsoft.SharePoint.SPFieldMultiLineText
    Microsoft.SharePoint.SPFieldNumber
    Microsoft.SharePoint.SPFieldPageSeparator
    Microsoft.SharePoint.SPFieldRecurrence
    Microsoft.SharePoint.SPFieldText
    Microsoft.SharePoint.SPFieldUrl