2

I'm using C# Windows application to retrieve all files names from SharePoint Site, I have gone through below answer

How to get all the files inside the folder in list

it's working perfectly for 1 folder, but I need to make a recursion to read all files from all folders, the file name with extension is enough for me, any Ideas ?

2 Answers 2

0

You use a CAML Query like this to get all documents in a library. Setting the Scope attribute to RecursiveAll tell SharePoint to go through all folders.

<View Scope='RecursiveAll'><Query><Where><Neq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Neq></Where></Query></View>

You would need to run this query once for each document library and aggregate the results if you want all documents in the site.

For more information on the use of CAML Queries to retrieve items, see: How to: Retrieve List Items

0

You can use the below PowerShell script to get all files from one Document library.

#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"
      
#Config Parameters
$SiteURL= "https://echodu.sharepoint.com/sites/sitename"
$LibraryName = "libraryname"
#$CSVPath = "C:\Temp\DocumentsInventory.csv"
  
#Get 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 and List
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($LibraryName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Prepare the query
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "@
    <View Scope='RecursiveAll'>
        <Query>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit Paged='TRUE'>2000</RowLimit>
    </View>"
  
    $Counter=1
    $DataCollection = @()
    #Batch Process items
    Do {
        #powershell sharepoint online list all documents
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
 
        #Iterate through each document in the library
        ForEach($ListItem in $ListItems| Where {$_.FileSystemObjectType -eq "File"})
        {
            #Display a Progress bar
            Write-Progress -Activity "Scanning Files in the Library" -Status "Collection file's Inventory '$($Item.FieldValues.FileRef)' ($Counter of $($List.ItemCount))" -PercentComplete (($Counter / $List.ItemCount) * 100)
 
            #Collect data       
            $DataCollection += New-Object PSObject -Property ([Ordered] @{
                FileName  = $ListItem.FieldValues["FileLeafRef"]
                RelativeURL = $ListItem.FieldValues["FileRef"]
                CreatedBy = $ListItem.FieldValues["Created_x0020_By"]
                CreatedOn = $ListItem.FieldValues["Created"]
                ModifiedBy = $ListItem.FieldValues["Modified_x0020_By"]
                ModifiedOn = $ListItem.FieldValues["Modified"]
                FileSize = $ListItem.FieldValues["File_x0020_Size"]
                
            })
            $Counter++
        }
    }While($Query.ListItemCollectionPosition -ne $Null)
    
    $DataCollection
    #Export Documents data to CSV
    #$DataCollection | Export-Csv -Path $CSVPath -Force -NoTypeInformation
    #Write-host -f Green "Documents Data Exported to CSV!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Reference:

SharePoint Online: PowerShell to List All Files Inventory in a Document Library

1
  • Is there anything else I can help with regarding this issue? You can comment us at any time and we will continue to follow up. Mar 28, 2022 at 5:57

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.