Iterating through a SharePoint List and Items with PowerShell

MichaelF
  • Iterating through a SharePoint List and Items with PowerShell MichaelF

    I am trying to iterate over a List and view properties on some items that live within that list, and down one or two levels; this is on a SharePoint 2010 server. So far I have been able to connect to the site and iterate over the Folders and the Items in the top level, but what I want is to be able to iterate over items within the Folders. Out structure is as follows:

    site - http://localhost/my_site list - Pages Folder - News ItemB - Article ItemA - Contents

    The site is easy enough to connect to, with that I can get the list, Folder and ItemA's and output them so I can see what lives there. What I would like is to be able to view all the ItemB's (and there may be lots of them) under each Folder, there are about 10 Folders in the List with each Folder containing lots of ItemB's.

    What's the easiest way to be able to find the Folder and then search under the Folder and get all of its Items to display? My experience with PowerShell and SharePoint in this regard is kind of limited. Any helpful pointers are appreciated.

    Here is what I have so far, I'd like to have the part that outputs Content to note the ItemB's.

    [string]$url = "http://localhost/my_site"
    $totalCount = 0
    
    $site = New-Object Microsoft.SharePoint.SPSite($url) 
    
    $web = $site.OpenWeb()
    
    $list = $web.Lists["Pages"]
    
    foreach ($folderItem in $list.Folders)
    {
      Write-Host "Folder: " $folderItem.Name "Count: " $folderItem.Folder.ItemCount
      $totalCount += $folderItem.Folder.ItemCount
    }
    
    foreach ($listItem in $list.Items)
    {
        Write-Host "    Content: " $listItem.Name "  Folder: " $listItem.Parent
    }
    
    Write-Host "Total Items = " $totalCount
    $totalFolders = $list.Folders.Count
    Write-Host "in a Total Folders of: " $totalFolders
    $site.Dispose() 
    

  • Try this method. It will retrieve items from a specified subfolder in a list

    function Get-SPFilesInFolder
    {
        [CmdletBinding()]
        param(
        [Parameter(Mandatory=$true)]
        [Microsoft.SharePoint.SPWeb]
        $site,
        [Parameter(Mandatory=$true)]
        [string]
        $listName,
        [Parameter(Mandatory=$true)]
        [string]
        $folderName
    
        )
    
        $list = $site.Lists | where{$_.Title -eq $listName} 
    
        if(!$list)
        {
            Write-Host "List not found"
            return
        }
    
        $query = New-Object Microsoft.SharePoint.SPQuery
        $query.Folder = $list.RootFolder.SubFolders[$folderName]
    
        $items = $list.GetItems($query)
    
        return $items
    }
    
    Clear-Host
    $gc = Start-SPAssignment
    
    $site = $gc | Get-SPWeb "http://intranet.aabnet.dk"
    $items = Get-SPFilesInFolder $site "Documents" "subfolderA"
    Write-Host $items
    $items | fl url
    
    $gc | Stop-SPAssignment
    

  • Anders that works pretty nice...since I am scanning folders under my Root which is called Pages, which has multiple folders in it, if I try to plug your code in I don't seem to be able to get the content under each folder.

    $site = New-Object Microsoft.SharePoint.SPSite($url) 
    $web = $site.OpenWeb()
    $list = $web.Lists["Pages"]
    foreach ($folderItem in $list.Folders)
    {
      Write-Host "Folder: " $folderItem.Name "Count: " $folderItem.Folder.ItemCount
      $totalCount += $folderItem.Folder.ItemCount
      $query = New-Object Microsoft.SharePoint.SPQuery
      $query.Folder = $list.RootFolder.SubFolders[$folderItem]
      $items = $list.GetItems($query)
      Write-Host "    Content: "
      $items | fl url
    }
    

    Is there something else I would need to do to scan each folder as I cycle through it?

Tags
powershell
Related questions and answers
  • We have taken over a site where My Sites are placed in the Intranet web application. I want to migrate the My Sites to a dedicated host site. From what i have been able to find out so far, I will need to do an STSADM -o backup of each personal site and restore it into the new content database. I will be automating this process using PowerShell. Has anyone tried this? Any caveats i need to be aware of? Since i need to move site collections out of an existing content database into another, as far as i can see i cannot use a simple SSP migration. Any help appreciated :-) EDIT: I wrote

  • /Pages/Example.aspx When I "View All Site Content" I am looking at the contents of the "Pages" list: http://myserver:PORT/subsite/Pages/Forms/AllItems.aspx ...(I have also asked this question on Stack Overflow) I have a SharePoint feature I'm using to add some custom aspx files to the Pages Library. When I activate the feature, I can visit the pages in-browser and they are visible in SPDesigner, but when I "View All Site Content" they are not there. Why is this the case? The Feature's Elements File: <?xml version="1.0" encoding="utf-8"?> <

  • So I did create on an SPWeb 20 custom lists and then a tasks list. For each of those 20 lists I've created 20 site lookup columns. I then created 20 Content Types based on Task content type. Then I've added each content type to the Tasks list. After that I've added each lookup site column to the corresponding list content type. So far so good. I've built a custom WebPart that displays task items... = list; SPQuery query = new SPQuery(view); System.Text.StringBuilder sbViewFields = new System.Text.StringBuilder(); // all fields from view foreach (string field in view.ViewFields

  • Using the following code I should be able to access the Taxonomy Term Stores: $site = new-object Microsoft.SharePoint.SPSite("http://localhost") $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site) $session.TermStores.Count Yet the TermStore count is always 0. I know I have Term Stores and Term Groups and Terms because I'm able to tag items using the terms and view them in the Term Store Management Tool. For the SPSite I have tried localhost, the actual computer name, and even the Central Admin site. Based on my research this shouldn't be that difficult. http

  • because my code had fetched the GUID of the list just milliseconds earlier! Can anyone explain what is going on here? I had the same problem yesterday and at some point it just went away...); System.Console.WriteLine(meta.Title); } } } As you can see, I fetch all the lists using GetListCollection and then, if it is a survey, I try to get the List...I'm have some trouble using the GetList web service. It does work sometimes but not at the start of the day!?! Here is my code: public static void ListSurveys(string serverName

  • I've been hitting my head against a wall on this one for the last couple of days. I have a pretty simple requirement but I cant see how it can be achieved within a Sandboxed solution. I'm depressed..., and then as part of that activation, drop web parts onto some specific pages. In my case I want to be able to drop it onto the "AllItems.aspx" page of a list, but I'm equally having trouble adding it to the default.aspx home page. I would be happy either way. Note: I want to be able to activate this on existing site, I dont want to create a new Site Definition. Here is what I have tried: Feature Receiver

  • I’ve been trying to use ListData.svc to get a REST feed from a list (actually a pages library) using something like: http://SharePoint/_vti_bin/ListData.svc/Pages?$orderby=NewsArticleDate&$filter=(2010 eq year(NewsArticleDate)) and (06 eq month(NewsArticleDate)) and (ContentType eq 'News Article') It all works great and our front-end developers have been able to combine this feed... on anonymous access (anonymous users can browse to the list items) this feed simply returns an error: Object reference not set to an instance of an object. Is it just not possible to use this service

  • setting in the web.config file to use the 3des algorithm as I had found on many sites. This fixed most of the issues except for my custom forms .ascx pages as everyone of them still give a FIPS error and they do not work. I have found the code that I can place in the machine.config file that turns off the check, but I would like to figure out what is going on with this. I have ensured that debug is set to false in the web.config file as well. It is only my user controls that are causing the issue, but I do not know why. So it would be nice if there was a list of known .Net assembles that were

  • within the SharePoint front end. All of the web parts will need to automatically detect consumers and providers to create connections at runtime. What we are looking to do is have webparts discover and automatically connect to other compatible webparts. When a user adds the two compatible parts to a page, the parts should be able create the appropriate connections. We have a well defined... purposes "best way" means most efficient, elegant and/or standard. We'd like to follow established sharepoint design patterns as much as possible, but code efficiency is somewhat important. I've been able

Data information