Share via


SPField class

Represents a field in a list on a SharePoint Foundation Web site.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPField
    

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

Syntax

'Declaration
Public Class SPField
'Usage
Dim instance As SPField
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 Microsoft 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 method for changes to take effect in the database.

Examples

The following example is a console application that gets a reference to the Expires field in the Announcements list and sets the default expiration date to one week after an item is created.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = web.Lists.TryGetList("Announcements");
                    if (list != null)
                    {
                        SPField fld = list.Fields[SPBuiltInFieldId.Expires];
                        fld.DefaultFormula = "=TODAY()+7";
                        fld.Update();
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.Read();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("https://localhost")
            Using web As SPWeb = site.RootWeb
                Dim list As SPList = web.Lists.TryGetList("Announcements")
                If list IsNot Nothing Then
                    Dim fld As SPField = list.Fields(SPBuiltInFieldId.Expires)
                    fld.DefaultFormula = "=TODAY()+7"
                    fld.Update()
                End If
            End Using
        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

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.SPFieldGeolocation
    Microsoft.SharePoint.SPFieldGuid
    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