Get URL of All Files in SharePoint Online Document Library using PowerShell

Requirement: Get the URL of All Files in a SharePoint Online Document Library using PowerShell

Get URLs of All Files in a SharePoint Online Document Library using PowerShell CSOM
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"
  
#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()  

    #Get All Items from the List
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
           
    $DataCollection = @()
    #Loop through each item 
    ForEach($ListItem in $ListItems)
    {
        #Calculate Absolute URL
        If($Web.ServerRelativeUrl -eq "/")
        {
            $AbsoluteURL=  $("{0}{1}" -f $Web.Url, $ListItem.FieldValues["FileRef"])
        }
        else
        {
            $AbsoluteURL=  $("{0}{1}" -f $Web.Url.Replace($Web.ServerRelativeUrl,''), $ListItem.FieldValues["FileRef"])
        } 

        #Collect data        
        $Data = New-Object PSObject -Property @{
            FileName  = $ListItem.FieldValues["FileLeafRef"]
            RelativeURL = $ListItem.FieldValues["FileRef"]
            AbsoluteURL = $AbsoluteURL
        }
        $DataCollection += $Data
    }
    $DataCollection | Format-List
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

PnP PowerShell to Get File URL in SharePoint Online
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Items from the list - In batches of 500
$ListItems = Get-PnPListItem -List $ListName -PageSize 500

#Loop through List Items and Get File URL
$Results=@()
ForEach($Item in $ListItems)
{
    $Results += New-Object PSObject -Property @{
    FileName = $Item.FieldValues['FileLeafRef']
    FileURL = $Item.FieldValues['FileRef']
    }
}
$Results
Get URL of All Files in SharePoint Online Document Library using PowerShell Get URL of All Files in SharePoint Online Document Library using PowerShell Reviewed by Salaudeen Rajack on May 08, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.