Khaine775
Khaine775

Reputation: 2765

Getting a list of items from document library using PowerShell in SharePoint

So if I have the URL for a Document Library, how can I get all the items (in this case, folders) from inside the library? I've tried a few different things, but I can't seem to get it.

This is what I've tried so far:

$web = Get-SPWeb($completeUrl)
$spDocumentLibrary = $completeUrl + "/DocumentLibrary"
$spSite = Get-SPSite -Identity http://url.com
$website = $spSite.OpenWeb()
$listItem = $website.GetListItem($completeUrl)

And it fails with an Exception calling "GetListItem" with "1" argument(s)...

I'm not reeaally sure where to go from here. What I'm interested in is getting all the folders I have in the document library, which I know is found at $completeUrl + /DocumentLibrary which would equal something like http://url.com/../../DocumentLibrary for example. But I'm apparently going at it wrong.

Any hints?

Upvotes: 0

Views: 23465

Answers (3)

S. Bishop
S. Bishop

Reputation: 11

So what I had to do was call $items at the end to get the results. I initially thought the $list.GetItems() was going to return the list of items.

So my script looks like this:

Add-PSSnapin Microsoft.SharePoint.PowerShell
$spDocumentLibrary = $completeUrl + "/DocumentLibrary"
$spSite = Get-SPSite -Identity https://url.com
$website = $spSite.OpenWeb()
$list = $website.Lists["List_Name_Here"]
$items = $list.GetItems()
$items | select -property Title,DisplayName,Url | `
Export-CSV -Path "\\DesiredLocation\file.csv" -NoTypeInformation

As you can see I piped my results to a CSV file. Just wanted to note the following things.

  1. For this to work for you change the generic https://url.com to the legit URL that you are trying to get the results from.
  2. Change the Export-CSV -Path "DesiredLocation" to an actual file path (i.e. c:\users\yourprofile\desktop\filename.csv
  3. Change the CSV FileName to something that makes sense to you.

Upvotes: 0

Marco
Marco

Reputation: 23935

This will print out all folder names of a document library:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue    
$web = Get-SPWeb 'http://urltoyourweb'
$list = $web.Lists['DocumentLibrary']

function ListFolders($folder) {
    Write-Host $folder.Name
    $folder.Subfolders | % { if( $_.item -ne $null) { ListFolders $_}}
}

ListFolders $list.RootFolder

Explanation:

ListFolders is a function, that calls itself, if a subfolder is not null. This is called a recursive function. Now, we supply it with the root folder of a SPList and then iterate over its subfolders collection (% is short for For-Each)

If this is too much inline piping for your taste, you could always write the more readable version of it:

##Same as $folder.Subfolders | % { if( $_.item -ne $null) { ListFolders $_}}
foreach($sfolder in $folder.SubFolders)
{
    if ($sfolder.item -ne $null)
    {
        ListFolders $sfolder
    }
}

Upvotes: 3

Deptor
Deptor

Reputation: 323

I do not know your SharePoint library construction, however, from my point of view, you are missing list selection.

$spDocumentLibrary = $completeUrl + "/DocumentLibrary"
$spSite = Get-SPSite -Identity http://url.com
$website = $spSite.OpenWeb()
$list = $website.Lists["List_Name_Here"]
$items = $list.GetItems()

Upvotes: 1

Related Questions