SPField Class
SPField Class (Microsoft.SharePoint)
Represents a field in a list on a Windows SharePoint Services Web site.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPField
Visual Basic (Usage)
Dim instance As SPField
C#
[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.

Visual Basic
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 
C#
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>";
            }
        }
    }
}
NoteNote:

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.

System.Object
  Microsoft.SharePoint.SPField
     Derived Classes
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
VB Language Filer Broken      S J ... Noelle Mallory - MSFT   |   Edit   |   Show History

Visual Basic example doesn't pay attention to my language filter. I have my filter set to C# only, but the example for Visual Basic appears, too.

It might have something to do with the text--it is listed as " [Visual Basic]", while the VB code blocks that are correctly filtered out are listed as "Visual Basic" (i.e. without the square brackets). Just a guess...

Bug : Code is C# does something else that VB      SandeepKNahta   |   Edit   |   Show History
VB code seems ok here but C# code seems trying to do something else here..
Tags What's this?: bug (x) Add a tag
Flag as ContentBug
Outdated code in C# example, differences in example function      James Love   |   Edit   |   Show History
Use of modern 'foreach' structure would make the C# example more legible.

VB example is "setting" field values, C# example is "getting" them, displaying them onto perhaps a Windows Form or a Web Form.
Microsoft.SharePoint.SPSimpleItem.get_Xml()      Charlie Holland   |   Edit   |   Show History
When inheriting from SPField in order to create a custom field, you may encounter the above error.

In order to resolve this probelm you must override FieldValueType.

For more info see: http://www.chaholl.com/archive/2009/07/17/microsoft.sharepoint.spsimpleitem.get_xml.aspx
Tags What's this?: Add a tag
Flag as ContentBug
SPField ListView rendering      Charlie Holland   |   Edit   |   Show History
When rendering a custom field in a list, none of the methods of SPField or your derived object are called. Instead the RenderPattern thats defined in your feild definition is used. This limits the control you have over how your field will be displayed. For more information, here's a link to an article that covers this in more detail: http://www.chaholl.com/archive/2009/11/07/whatrsquos-in-a-renderpattern.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker