2

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?

UPDATE 1:

So with a little inspiration from John below, I've modified my script:

$web = Get-SPWeb($completeUrl)
$spDocumentLibrary = $completeUrl + "/DocumentLibrary"
$site = New-Object Microsoft.SharePoint.SPSite($spDocumentLibrary)
$website = $site.OpenWeb()
$list = $website.GetList($spDocumentLibray)

This gets me the wanted document library, now I'm going for all the items inside it.

UPDATE 2:

    foreach($item in $list) {
        write-host "listItem: " $item.Title
    }

This just gets me the same as $list. Any ideas to what I'm doing wrong?

UPDATE 3:

It seems I also had to include the following to be able to do the foreach above:

$items = $list.GetItems()
3

I haven't tested this but should be something like that I think,

$web = Get-SPWeb($completeUrl)

$spDocumentLibrary  = $completeUrl + "/DocumentLibrary"  
//do you also need to put a /lists before the list name I wonder(if it's a list)

$list = $web.GetList($spDocumentLibrary)
$items = $list.GetItems()

foreach ($item in $items)
{
//do whatever you need to do
}
  • Thank you for your input. Now I'm able to get all the items inside the document library, do you have a suggestion to how to get the underlying folders inside the document library instead of just the items? – Khaine775 Aug 18 '16 at 7:50
  • this seem to work: foreach ($folder in $list.Folders) { $folder } – ova Aug 18 '16 at 8:16
1

GetListItem() only gets you one specific item from the list and your URL points to the list only, not to an item.

Try first to use GetList() to get the List object, then you should be able to walk through the list or do a Query on the items in the list.

  • Thanks for the input. I've edited the original post to contain what I added to make it work. I've tried adding a foreach to write out the items but it just gets me the same single item Documentlibrary as the $list also contains. – Khaine775 Aug 18 '16 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.