Partilhar via


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
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
'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 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
)

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.

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

Exceptions

Exception Condition
InvalidOperationException

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

Examples

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

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

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

    collViewFields.Add("Field1_Name");
    collViewFields.Add("Field2_Name");
    collViewFields.Add("Field3_Name");

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

    collViews.Add(strViewName, collViewFields, strQuery, 100, true, 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