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'm trying to loop all splistitem in a folder of a library. I can get my library and have folder but i'm not able to get the items inside the folder. When i display the listitem for folder, i've the number of folder at root library and inside folder not the splistitem in folder. Here is what i did.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$url = "http://myspurl"
$SPWeb = Get-SPWeb $url
#retrieve list to update
$list = $SPWeb.Lists["myList"]
write-host $list.Title
#loop in lib
foreach($folder in $list.Folders)
{
if($folder.Name -eq "10002")
{
    #this count return me always the number of folder at the root of library and inside folder. and not the splistitem inside folder
    $folder.ListItems.Count
    foreach($item in $folder.ListItems)
    {
        #write-host "TEST"
        #$item = $file.Item
        #write-host "item = "$item.Title
        #$item.Url
    }

}
}

When i'll have splistitem inside folder i've to move it in an other folder depending of a meta

Thanks for your help

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The objects you get from $list.Folders are SPListItems not SPFolders so the ListItems property gives the Lists ListItemCollection not something for the folder.

To get the SPFolder you have to access $folder.Folder.

If what you're working with is a document library, then it's easy just access $folder.Folder.Files and you'll get the files in the SPFolder, then for each file you can get the Item

But if you're working with a list then you have to use a SPQuery to get the listitems in the folder like this:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$url = "http://myspurl"
$SPWeb = Get-SPWeb $url
#retrieve list to update
$list = $SPWeb.Lists["myList"]
write-host $list.Title
#loop in lib
foreach($folder in $list.Folders)
{
  if($folder.Name -eq "10002")
  {
    $query = New-Object -Type 'Microsoft.SharePoint.SPQuery'
    $query.Folder = $folder.Folder
    $folderItems = $list.GetItems($query)
    foreach($item in $folderItems)
    {
      write-host $item.Title
    }
  }
}
share|improve this answer
    
great solution thanks a lot with a working sample –  bender Apr 26 at 14:42

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.