Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (5)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
SPFieldCollection Class (Microsoft.SharePoint)
Represents a collection of SPField objects.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<DefaultMemberAttribute("Item")> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPFieldCollection
    Inherits SPBaseCollection
Visual Basic (Usage)
Dim instance As SPFieldCollection
C#
[DefaultMemberAttribute("Item")] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public class SPFieldCollection : SPBaseCollection

Use the Fields property of either the SPList class or the SPListItem class to return the collection of fields for a list or list item. Use the Fields property of the SPWeb class to return the fields in the Web site, and use the Fields property of the SPContentType class to get the fields that are associated with the content type. To create a field, use the Add method.

Use an indexer to return a single field from the collection. For example, assuming the collection has been assigned to a variable named collFields, use collFields[index] in Microsoft 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.

The following code example adds a new text field to the Announcements list of a specified Web site that is displayed in the default view of the list.

Visual Basic
Dim webSite As SPWeb = SPContext.Current.Site.AllWebs("MySite")
Try
    Dim list As SPList = webSite.GetList("Lists/Announcements/AllItems.aspx")
    Dim fields As SPFieldCollection = list.Fields

    Dim newFieldName As String = fields.Add("MyNewField", SPFieldType.Text, False)

    Dim fieldAdd As SPField = fields.GetField(newFieldName)

    Dim view As SPView = list.DefaultView
    Dim viewFields As SPViewFieldCollection = view.ViewFields
    viewFields.Add(fieldAdd)
    view.Update()
Finally
    webSite.Dispose()
End Try
C#
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["MySite"])
{
    SPList oList = oWebsite.GetList("Lists/Announcements/AllItems.aspx");
    SPFieldCollection collFields = oList.Fields;

    string strNewFieldName = collFields.Add("MyNewField", SPFieldType.Text, false);
    SPField oField = collFields.GetField(strNewFieldName);

    SPView oView = oList.DefaultView;
    SPViewFieldCollection collViewFields = oView.ViewFields;
    collViewFields.Add(oField);
    oView.Update();
}
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.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Update the source list      diffident   |   Edit   |   Show History
One needs to call the update method of the source list to which the new field has been added for the new field to appear.

oList.Update();

At least, that's what I had to do in my case.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker