Export (0) Print
Expand All

SPField Class

Windows SharePoint Services 3

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

System.Object
  Microsoft.SharePoint.SPField
    

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

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

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.

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

Community Additions

ADD
Show:
© 2014 Microsoft