Quantcast
Channel: SharePoint 2013 - Setup, Upgrade, Administration and Operations forum
Viewing all articles
Browse latest Browse all 21070

List View Threshold - Caml Query

$
0
0

I am trying to help a school trim the items in their sub site's main calendar, because it is over the 5000 item limit, and users can no longer sync the calendar to Outlook to more easily manage events.

Below is the relevant snip from the PowerShell code I'm using to try to delete the items from SharePoint Online programmatically (identifying information replaced with bracketed descriptors):

$url = "https://[district domain].sharepoint.com/sites/[school name]"
$listName = "[Calendar Name]"

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = $creds

$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()

$list = $web.Lists.GetByTitle($listName)
$ctx.Load($list)
$ctx.ExecuteQuery()

Write-Output ("List is: '" + $list.Title + "' with item count " + $list.ItemCount)

$caml = 
'<Query><Where><And><And><Gte><FieldRef Name="ID"></FieldRef><Value Type="Number">0</Value></Gte><Lt><FieldRef Name="ID"></FieldRef><Value Type="Number">2000</Value></Lt></And><Leq><FieldRef Name="Created" /><Value IncludeTimeValue="FALSE" Type="DateTime">2011-12-31</Value></Leq></And></Where></Query>'

$query = new-object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = $caml

$items = $list.GetItems($query)
$ctx.Load($items)
$ctx.ExecuteQuery() # Error occurs at this statement

The output is:

List is: '[Calendar Name]' with item count 5376

Exception calling "ExecuteQuery" with "0" argument(s): "The attempted operation is prohibited because it exceeds the list 
view threshold enforced by the administrator."
At C:\Powershell\Sharepoint\Delete-List-Items-Older-Than.ps1:112 char:1+ $ctx.ExecuteQuery()+ ~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : ServerException


As for the CAML query itself:  For the first condition, I have tried FieldRef "MasterSeriesItemID" as well as "ID" (Settings -> Indexed Columns for the calendar lists "MasterSeriesItemID", but I saw code elsewhere using"ID" for the FieldRef of a SharePoint list).  I have tried removing the "Created" condition altogether, since I indexed that column after the list passed the list view threshold, and I'm not sure that SharePoint sees it as indexed, even though it now shows up in the list of indexed columns.

Even with only the condition that the ID / MasterSeriesItemID be between 0 and 2000, the code still fails with the same error.

My understanding is that the list view threshold can be avoided by querying an indexed column and filtering to <= 5000 items on that column.

Is there another FieldRef Name I should use for the query?  The original three indexed columns, according to SharePoint, were:

1. End Time
2. MasterSeriesItemID
3. Start Time (Automatically created)

I can't very well do this by hand in the web interface, when there are multiple schools with calendars over the limit, some by over 1000 items.  Unless the ID / MasterSeriesItemID is an auto-increment column starting at 0, and thus tracks closely with the Created date of the item, I'd prefer to use the Created date to do the trimming.

Thanks for any help.


Viewing all articles
Browse latest Browse all 21070

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>