This code is a Powershell script that will print out list items in a treeview manner. You will have to pass the below two Parameters in order to work with a specific list and get its items.
$stringUrl – the url of the SharePoint site.
$stringListName – the name of the list
Here is the script
#============================================================
# Function Get-FolderItems is a recursive function that will
# get all items from a list and print it out like a Treeview
#============================================================
function Get-FolderItems([Microsoft.SharePoint.SPFolder]$folder, $level) {
#The following code is to simulate the treeview in the printout. It if for visual purposes only
if($level -eq $null) {
$level=0
}
$prestring = ""
for($x=0;$x -le $level;$x++) {
$prestring += "—"
}
# Create query object
$query = New-Object Microsoft.SharePoint.SPQuery
$query.Folder = $folder
# Get SPWeb object
$web = $folder.ParentWeb
# Get SPList
$list = $web.Lists[$folder.ParentListId]
# Get a collection of items in the specified $folder
$itemCollection = $list.GetItems($query)
# If the folder is the root of the list, display information
if($folder.ParentListID -ne $folder.ParentFolder.ParentListID) {
Write-Host($folder.Name + " [Root of the list with " + $folder.ItemCount + " item(s) in it]")
}
# Iterate through each item in the $folder – Note sub folders and list items are both considered items
foreach($item in $itemCollection) {
# If the item is a folder
if($item.Folder -ne $null) {
# Write the Subfolder information
Write-Host($prestring + "*" + $item.Name + " [Folder Item | Parent Folder: " + $folder.Name + " | Contains " + $folder.ItemCount + " item(s)]")
# Call the Get-Items function recursively for the found sub-solder
Get-FolderItems $item.Folder ($level+1)
}
# If the item is not a folder
if($item.Folder -eq $null) {
# Write the item information
Write-Host($prestring + $item.Name + " [Item | Parent Folder: " + $folder.Name + "]")
}
}
$web.dispose()
$web = $null
}
#============================================================
# Function Get-Folder will return a SPFolder object based on
# $webUrl string and a $listName string
#============================================================
function Get-Folder([string]$webUrl,[string]$listName) {
# Use the Get-SPWeb CMDLET – it gives you the SPWeb object without having to create the SPSite object
$web = Get-SPWeb $stringUrl
# Get your SPList
$list = $web.Lists[$stringListName]
# Get the root folder containing items in your list
$folder = $list.RootFolder
$web.Dispose()
$web = $null
return ,$folder
}
#============================================================
# Main procedural code
#============================================================
$stringUrl = "http://yourSPSiteurl"
$stringListName = "Your List Name"
# Get the root folder for the List
$folder = Get-Folder $stringUrl $stringListName
# Call the recursive Get-FolderItems function passing the root of your list. It will call itself again and again until it has gone through your entire structure
Get-FolderItems $folder