Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

I don't know how I missed the answer to this before, but I had to remove the <query> and </query> from the $query string.

However I now noticed that this works on some libraries and not others?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.