Share via


SPViewCollection.Add method (String, StringCollection, String, UInt32, Boolean, Boolean)

Creates a view in the collection with the specified name, view fields, query, row limit, and Boolean values that specify whether the view displays items page by page and whether it is the default view.

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

Syntax

'Declaration
Public Function Add ( _
    strViewName As String, _
    strCollViewFields As StringCollection, _
    strQuery As String, _
    iRowLimit As UInteger, _
    bPaged As Boolean, _
    bMakeViewDefault As Boolean _
) As SPView
'Usage
Dim instance As SPViewCollection
Dim strViewName As String
Dim strCollViewFields As StringCollection
Dim strQuery As String
Dim iRowLimit As UInteger
Dim bPaged As Boolean
Dim bMakeViewDefault As Boolean
Dim returnValue As SPView

returnValue = instance.Add(strViewName, _
    strCollViewFields, strQuery, iRowLimit, _
    bPaged, bMakeViewDefault)
public SPView Add(
    string strViewName,
    StringCollection strCollViewFields,
    string strQuery,
    uint iRowLimit,
    bool bPaged,
    bool bMakeViewDefault
)

Parameters

  • strViewName
    Type: System.String

    A string that contains the name of the view.

  • iRowLimit
    Type: System.UInt32

    The maximum number of items to return in the view. Specifying a value greater than Int32.MaxValue (2,147,483,647 or hexadecimal 0x7FFFFFFF) throws an exception because the value is out of range.

  • bPaged
    Type: System.Boolean

    true to specify that the view supports displaying more items page by page; otherwise, false.

  • bMakeViewDefault
    Type: System.Boolean

    true to make the view the default view; otherwise, false.

Return value

Type: Microsoft.SharePoint.SPView
The new view.

Examples

The following code example creates a view that returns items where the value of a field equals a specified text value.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
Dim list As SPList = site.Lists("List_Name")
Dim views As SPViewCollection = list.Views

Dim viewName As String = "View_Name"

Dim viewFields As New System.Collections.Specialized.StringCollection()

viewFields.Add("Field1_Name")
viewFields.Add("Field2_Name")
viewFields.Add("Field3_Name")

Dim query As String = "<Where><Eq>
        <FieldRef Name='<iterm>Field3_Name</iterm>'/>" _ 
    & "<Value Type='Text'><iterm>Text</iterm></Value></Eq></Where>"

views.Add(viewName, viewFields, query, 100, True, False)
SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
{
    SPList oList = oWebsite.Lists["List_Name"];
    SPViewCollection collViews = oList.Views;
    string strViewName = "View_Name";
    System.Collections.Specialized.StringCollection collViewFields = new System.Collections.Specialized.StringCollection();
    collViewFields.Add("Field1_Name");
    collViewFields.Add("Field2_Name");
    collViewFields.Add("Field3_Name");
    string strQuery = "<Where><Lt><FieldRef Name=\"Field3_Name\"/>" +
    "<Value Type=\"Integer\">1000</Value></Lt></Where>";

    collViews.Add(strViewName, collViewFields, strQuery, 100, true, false,
    Microsoft.SharePoint.SPViewCollection.SPViewType.Grid, false);
}

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

SPViewCollection class

SPViewCollection members

Add overload

Microsoft.SharePoint namespace