0

I'm wanting to iterate through a Document Library in SharePoint Online and have located this as a previous entry -

Traverse through List of Documents using PowerShell

However, digging in the parameters for SPOSite I can't see a list parameter so can't adapt the linked script to work in a SharePoint Online environment. Is there another route of getting this information?

2 Answers 2

2

Run following commands with SharePoint Online Management Shell

$webUrl = Read-Host -Prompt "HTTPS URL for your SP Online 2013 site"

$username = Read-Host -Prompt "Email address for logging into that site"

$password = Read-Host -Prompt "Password for $username" -AsSecureString

$folder = "Documents" $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)

$ctx.Credentials =New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$password)

$web = $ctx.Web $lists = $web.Lists.GetByTitle($folder)

$query =[Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000)

$result = $lists.GetItems($query)

$ctx.Load($Lists)

$ctx.Load($result)

$ctx.ExecuteQuery()

foreach ($File in $result) { Write-host "Url: $($File["FileRef"]), title: $($File["FileLeafRef"]) "}

2

I have a commandlet in our module that I use to pull library file counts, I don't know exactly what you are trying to accomplish, but could be a useful base. You pass it in a URL and it will iterate over every list.

function Get-AllListItemCountsForWeb{
    <#
        .SYNOPSYS
           Generates a report of all lists and their item counts for a web
        .DESCRIPTION
           Generates a report of all lists and their item counts, for a web, excludes APP sites
        .PARAMETER WebUrl
           The url of the site to generate the report on
        .PARAMETER GridView
            If this switch is used, it will generate a grid view instead of an export            
        .EXAMPLE
           Generates a CSV export
           Get-AllListItemCountsForWeb -WebUrl "https://tenant.sharepoint.com/teams/eric"
        .EXAMPLE
            Opens a filterable grid view
            Get-AllListItemCountsForWeb -WebUrl "https://tenant.sharepoint.com/teams/eric" -GridView
    #>
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$true, HelpMessage="The URL to the site that contains the list or library", Position=0)]
        [string]$WebUrl,
        [Parameter(Mandatory=$false, Position=1)]
        [switch]$GridView
    )

    Begin{
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl) 
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
        $web = $context.Web
        $lists = $web.Lists
        $context.Load($web)
        $context.Load($lists)
        $context.ExecuteQuery()      
    }
    Process{
        $Result = @()
        if ($web.WebTemplate -ne "APP"){
            foreach($list in $lists){
                $count = Get-ListItemCount -WebUrl $WebUrl -ListName $list.Title
                $Result += New-Object PSObject -Property @{
                    'Library Title' = $list.Title
                     Count = $count
                     'Site Title' = $web.Title
                     URL = $WebUrl
                     'Library Type' = $list.BaseType
                }
            }
        }
    }
    End{
        if($GridView){
            $Result | Select 'Site Title',URL,'Library Type','Library Title',Count | Out-GridView
        }
        else{
            $filename = "C:\Users\$env:USERNAME\Desktop\"+$web.Title.Replace(" ", "_")+"_ListReport.csv"
            $Result | Select 'Site Title',URL,'Library Type','Library Title',Count | Export-Csv $filename -NoTypeInformation
            Write-Host "Report saved to $filename" -ForegroundColor Green
        }
        $context.Dispose()
    }
}

This in turn calls another function

function Get-ListItemCount{
    <#
        .SYNOPSYS
           Returns the Item Count for the specified list or library
        .DESCRIPTION
           Returns the Item Count for the specified list or library
        .PARAMETER WebUrl
            The URL to the site that contains the list or library
        .PARAMETER ListName
            The display name of the list or library to get a count for
        .EXAMPLE
           Get-ListItemCount -WebUrl "https://tenant.sharepoint.com/teams/eric" -ListName "Shared Documents"
    #>
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$true, HelpMessage="The URL to the site that contains the list or library", Position=0)]
        [string]$WebUrl,

        [Parameter(Mandatory=$true, HelpMessage="The display name of the list or library to get a count for", Position=1)]
        [string]$ListName
    )

    Begin{
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)

    }
    Process{
        try{
            $web = $context.Web
            $list = $web.Lists.GetByTitle($ListName)
            $context.Load($list)
            $context.ExecuteQuery()
            return $list.ItemCount
        }
        catch{
            Write-Host -ForegroundColor Red $_.Exception.Message
            return 0
        }

    }
    End{
        $context.Dispose()
    }
}
2
  • I'm starting to think I should be sending you drinks for all the help you provide. This is great and is close to what I'm after actually, so many thanks. I'm seeing things here that I didn't know about, which I'd like to understand more such as the Microsoft.SharePoint.Client.ClientContext object that both you and Viraj refer too. Is this made usable via the SPO Management Shell and what else might it be useful for? Dec 6, 2016 at 16:55
  • Microsoft.SharePoint.Client.ClientContext is the client side object model (CSOM), basically a requirement for using PowerShell against SPO as the SPO management shell is lacking. I've covered some of this over on ericjalexander.com. Dec 6, 2016 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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