Question

I get the view limit exception when I make a request to the SharePoint online api that has more than 5K items in the view. I want to be able to count the number of items in the view without returning those items (thereby bypassing the exception).

Here is the ViewXml template that I use:

<View>
  <RowLimit>1000</RowLimit>
  <Query>
    <Where>
      <And>
        <Geq>
          <FieldRef Name="{2}"></FieldRef>
          <Value Type="DateTime">{0:yyyy-MM-dd}</Value>
        </Geq>
        <Leq>
          <FieldRef Name="{2}"></FieldRef>
          <Value Type="DateTime">{1:yyyy-MM-dd}</Value>
        </Leq>
      </And>
    </Where>
  </Query>
</View>

Was it helpful?

Solution

The answer is "no". There is no way to check for the number of items returned by a View. There is List.ItemCount but it will return the number of items in the list. Unfiltered.

If you need the number of items returned by the View you'll need to get the items and count them. Use "paging" to get around the 5K-Limit.

  • You can use RowLimit in the CAML, to stay below 5K
  • CamlQuery, as well as ListItemCollection has a ListItemCollectionPosition which is null when all items are read..

... so you'd need something along the lines of:

$list = Get-MyCoolList
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View><Query><Where>...</Where></Query><RowLimit>4999</RowLimit></View>";
do {
  $items = $list.GetItems($query);
  $query.ListItemCollectionPosition = $items.ListItemCollectionPosition;

  #  ... do cool stuff...

} while ($query.ListItemCollectionPosition -ne $null)

OTHER TIPS

When the list contains more than 5000 items, the columns you are querying must be indexed.

https://www.codeproject.com/Articles/1076854/Overcoming-the-List-View-Threshold-in-SharePoint-C

This solves my problem, but doesn't answer my original question. I would still like to know if there is a way to get the count without having to download all the rows.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top