Partilhar via


SPViewFieldCollection.Add Method (String)

Adds the field with the specified name to the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
<ClientCallableMethodAttribute(ClientMethodEndCodeSnippet := "OnAdd({0});")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "fieldNameDoesNotExist", Condition := "The field does not exist in the list.",  _
    ErrorType := GetType(ArgumentException))> _
Public Sub Add ( _
    strField As String _
)
'Usage
Dim instance As SPViewFieldCollection
Dim strField As String

instance.Add(strField)
[ClientCallableMethodAttribute(ClientMethodEndCodeSnippet = "OnAdd({0});")]
[ClientCallableExceptionConstraintAttribute(FixedId = "fieldNameDoesNotExist", Condition = "The field does not exist in the list.", 
    ErrorType = typeof(ArgumentException))]
public void Add(
    string strField
)

Parameters

  • strField
    Type: System.String

    A string that contains the field name.

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

viewFields.Add("Field_Name")

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;
    collViewFields.Add("Field_Name");
    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.

See Also

Reference

SPViewFieldCollection Class

SPViewFieldCollection Members

Add Overload

Microsoft.SharePoint Namespace