SPQuery.RowLimit Property

Gets or sets a limit for the number of items returned in the query per page.

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

Syntax

'Declaration
Public Property RowLimit As UInteger
    Get
    Set
'Usage
Dim instance As SPQuery
Dim value As UInteger

value = instance.RowLimit

instance.RowLimit = value
public uint RowLimit { get; set; }

Property Value

Type: System.UInt32
An unsigned 32-bit integer that specifies the row limit.

Remarks

The RowLimit property contains a value that corresponds to the value of the RowLimit element in Collaborative Application Markup Language.

The RowLimit property is used together with the ListItemCollectionPosition property to define paging in a query. Specifically, the SPListItemCollectionPosition object that is held in the ListItemCollectionPosition property is used to iterate through all the items in a collection n items at a time, where n is the value specified as a row limit.

Note

For information about how to use Language-Integrated Query (LINQ) queries to retrieve list items in SharePoint Foundation, see Managing Data with LINQ to SharePoint.

Examples

The following code example uses the ListItemCollectionPosition properties of the SPListItemCollection and SPQuery classes to return an SPListItemCollectionPosition object for storing where each page of data ends in the collection of items and displays the titles of items in groups of 10 rows. The example assumes that the list is a document library or that folders are enabled in the list. The example also assumes that the list includes a field with the internal name “Field1” and a field with the internal name “Field2”.

This example requires using directives (Imports in Microsoft Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
Try
    Dim list As SPList = webSite.Lists("Announcements")
    Dim query As New SPQuery()
    query.RowLimit = 10    
    query.Query = "<OrderBy Override=\"TRUE\">" + _ 
        "<FieldRef Name=\"FileLeafRef\" /></OrderBy>";
    Dim i As Integer = 1

    Do
        Response.Write(("<BR>Page: " + i + "<BR>"))
        Dim listItems As SPListItemCollection = list.GetItems(query)

        Dim listItem As SPListItem
        For Each listItem In  listItems
            Response.Write((SPEncode.HtmlEncode(listItem("Title")) + _
              "<BR>"))
        Next listItem

        query.ListItemCollectionPosition = _
          listItems.ListItemCollectionPosition
        i += 1
    Loop While Not (query.ListItemCollectionPosition Is Nothing)
Finally
    webSite.Dispose()
End Try
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{
    SPList oList = oWebsiteRoot.Lists["Announcements"];
    SPQuery oQuery = new SPQuery();
    oQuery.RowLimit = 10;
    oQuery.Query = "<OrderBy Override=\"TRUE\">" +
         "<FieldRef Name=\"FileLeafRef\" /></OrderBy>";
    int intIndex = 1;

    do
    {
        Response.Write("<BR>Page: " + intIndex + "<BR>");
        SPListItemCollection collListItems = oList.GetItems(oQuery);

        foreach(SPListItem oListItem in collListItems)
        {
            Response.Write(SPEncode.HtmlEncode(oListItem["Title"]) + 
              "<BR>");
        }
        oQuery.ListItemCollectionPosition = 
          collListItems.ListItemCollectionPosition;
        intIndex++;
    } while(oQuery.ListItemCollectionPosition != null);
}

The next code example uses the RowLimit property to specify a limit of 50 items per page to return in the query.

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

Dim webSite As SPWeb = SPContext.Current.Site.AllWebs["Site_Name"]
Try
    Dim list As SPList = webSite.Lists("List_Name")

    Dim query As New SPQuery()
    query.ViewFields = "<FieldRef Name='Field1'/>" + _
        "<FieldRef Name='Field2'/>"
    query.RowLimit = 50
    Dim items As SPListItemCollection = list.GetItems(query)

    Dim item As SPListItem
    For Each item In  items
        Response.Write((SPEncode.HtmlEncode(item.Xml) + "<BR>"))
    Next item
Finally
    webSite.Dispose()
End Try
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Website_Name"])
{
    SPList oList = oWebsite.Lists["List_Name"];

    SPQuery oQuery = new SPQuery();
    oQuery.ViewFields = "<FieldRef Name='Field1'/>" +
        "<FieldRef Name='Field2'/>";
    oQuery.RowLimit = 50;
    SPListItemCollection collListItems = oList.GetItems(oQuery);

    foreach (SPListItem oListItem in collListItems)
    {
        Response.Write(SPEncode.HtmlEncode(oListItem.Xml) + "<BR>");
    }
}

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

SPQuery Class

SPQuery Members

Microsoft.SharePoint Namespace