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

Creates a view in the collection with the specified name, view fields, query, row limit, Boolean values specifying whether the view displays items page by page and whether it is the default view, the view type, and a Boolean value specifying whether the view is personal or public.

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


Public Function Add ( _
    strViewName As String, _
    strCollViewFields As StringCollection, _
    strQuery As String, _
    iRowLimit As UInteger, _
    bPaged As Boolean, _
    bMakeViewDefault As Boolean, _
    type As SPViewCollection.SPViewType, _
    bPersonalView As Boolean _
) As SPView
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 type As SPViewCollection.SPViewType
Dim bPersonalView As Boolean
Dim returnValue As SPView

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


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

  • bPersonalView
    Type: System.Boolean

    true to create a personal view; false to create a public view.

Return value

Type: Microsoft.SharePoint.SPView
The new view.


Exception Condition

The specified view type is not valid for the list template type.


The following code example creates a Grid view and displays items where a field value is less than 1,000.

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()


Dim query As String = "<Where><Lt><FieldRef Name='<iterm>Field3_Name</iterm>'/>" _
    & "<Value Type='Integer'>1000</Value></Lt></Where>"

views.Add(viewName, viewFields, query, 100, True, False, Microsoft.SharePoint.SPViewCollection.SPViewType.Grid, 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();


    string strQuery = "<Where><Eq><FieldRef Name=\"Field3_Name\"/>" +
        "<Value Type=\"Text\">Text</Value></Eq></Where>";

    collViews.Add(strViewName, collViewFields, strQuery, 100, true, false);


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


SPViewCollection class

SPViewCollection members

Add overload

Microsoft.SharePoint namespace