SPViewFieldCollection.Add method (SPField)
Adds the specified field to the collection.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'Declaration
Public Sub Add ( _
field As SPField _
)
'Usage
Dim instance As SPViewFieldCollection
Dim field As SPField
instance.Add(field)
public void Add(
SPField field
)
Parameters
field
Type: Microsoft.SharePoint.SPFieldAn SPField object that represents the field.
Remarks
The Add method requires that the Update method of the SPView class be called for changes to take effect in the database.
Examples
The following code example uses the Add method to make an existing field a view field for the specified list.
Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
Dim list As SPList = site.Lists("List_Name")
Dim view As SPView = list.Views("View_Name")
Dim viewFields As SPViewFieldCollection = view.ViewFields
Dim newViewField As SPField = list.Fields("Field_Name")
viewFields.Add(newViewField)
view.Update()
SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
{
SPList oList = oWebsite.Lists["List_Name"];
SPView oView = oList.Views["View_Name"];
SPViewFieldCollection collViewFields = oView.ViewFields;
SPField oViewField = oList.Fields["Field_Name"];
collViewFields.Add(oViewField);
oView.Update();
}
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 Disposing Objects.