I have a powershell script that I am running that executes a caml query to get some list items. The problem I am having is that whenever I execute this query in CAML Builder I am only getting 3 results (what I would expect). However using this same query in my powershell I am getting back all the documents (47 in this case).
I am pretty sure It has something to do with my powershell code but can't seem to figure it out.
Here is my code:
$SiteCollectionUrl ="http://sharepointsite/site"
$RootWeb = get-spweb $SiteCollectionUrl
foreach ($list in ($RootWeb.Lists | ? {$_.BaseTemplate.ToString() -eq "DocumentLibrary"}))
{
#######################################
#Build Caml Query
#######################################
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$query = '<Query><Where><And><IsNull><FieldRef Name="_vti_ItemDeclaredRecord" /></IsNull><IsNotNull><FieldRef Name="CheckoutUser" /></IsNotNull></And></Where></Query>'
$spQuery.ViewAttributes = "Scope = 'Recursive'"
$spQuery.Query = $query
$spQuery.RowLimit = $list.ItemCount
$spListItemCol = $list.GetItems($spQuery)
Write-Host $spListItemCol.Count
foreach($i in $spListItemCol)
{
#Process Items
}
}
Here is the caml query that I use in the builder:
<Query>
<Where>
<And>
<IsNull><FieldRef Name="_vti_ItemDeclaredRecord" /></IsNull>
<IsNotNull><FieldRef Name="CheckoutUser" /></IsNotNull>
</And>
</Where>
</Query>