Question

I am trying to pick the list of documents from certain folder in a document library. The folder contains huge set of documents. To optimize performance we have been asked to use SPQuery command in script to retrieve the file list. When I used the below script I got all the files under the specified library. Can anyone help me to prepare the query to point to the folder and the items only from that folder. In this case it is 'Folderone'.

$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spLibrary = $spListCollection.TryGetList($LibraryName)
$rfolder=$SPweb.Lists[$spLibrary].RootFolder.SubFolders["Folderone"]
$spQuery.Query ="<Where><BeginsWith><FieldRef Name='Folderone'/><Value Type='Folder'>Lib1/Folderone</Value></BeginsWith></Where>"
$spQuery.RowLimit = 200
do{
$folderItems = $splibrary.GetItems($spquery)
$spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach($item in $folderItems)
    {
        Write-Host $item.Name
    }    
}while ($spQuery.ListItemCollectionPosition -ne $null)
Was it helpful?

Solution

You have to update the code and add the below line of code. The problem was you were getting the specific folder but was not assigning it to SPQuery.

$rfolder=$SPweb.Lists[$spLibrary].RootFolder.SubFolders["Folderone"]
//Add the below line of code
$spQuery.Folder = $rfolder;
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top