none
Iterate through document libraries including subfolder

    Question

  • Hi guys,

    I'm trying to read the metadata for each file located in document libraries on SharePoint Online.

    it seems I'm missing something.

    I'm getting results back only on the $Item.Id and blanks on the rest of the properties!

    I'm not sure what I'm doing wrong here.

    could you please help?

    Here is my script.

    Clear-Host 
    $User = "JohnSmith@SmithCorp.com"
    $SecureFile = "C:\Users\JohnSmith\OneDrive - Smith Corporation\Hotels\PowerShell\MyPassword.txt"
    $Password = Get-Content $SecureFile | ConvertTo-SecureString 
    
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
    $Site = "https://smithcorp.sharepoint.com/bif"
    $DocLib = "Documents" 
    
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site) 
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password) 
    $Context.Credentials = $Credentials
    
    $List = $Context.Web.Lists.GetByTitle($DocLib) 
    $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $Items = $List.GetItems($Query)
    
    $Context.Load($List)
    $Context.Load($Items) 
    $Context.ExecuteQuery()
    
    
    foreach($Item in $Items)
    {    
        Write-Host "Item ID="$Item.Id " | Item File Name="$Item.file.Name
    }
    
    
    

    Here is PS results.


    Codernater

    Thursday, March 2, 2017 9:26 PM

Answers

  • Hi,

    For getting $Item.File, we need to run ExecuteQuery() again for getting this object. So I have modified the foreach part as below.

    You can use the demo below:

    $User = "JohnSmith@SmithCorp.com"
    $SecureFile = "C:\Users\JohnSmith\OneDrive - Smith Corporation\Hotels\PowerShell\MyPassword.txt"
    $Password = Get-Content $SecureFile | ConvertTo-SecureString 
    
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
    $Site = "https://smithcorp.sharepoint.com/bif"
    $DocLib = "Documents" 
    
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site) 
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password) 
    $Context.Credentials = $Credentials
    
    $List = $Context.Web.Lists.GetByTitle($DocLib) 
    $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $Items = $List.GetItems($Query)
    
    $Context.Load($List)
    $Context.Load($Items) 
    $Context.ExecuteQuery()
    
    foreach($Item in $Items)
    {   
        $File=$Item.File 
        $Context.Load($File) 
        $Context.ExecuteQuery()
        Write-Host "Item ID="$Item.Id " | Item File Name="$File.Name
    }
    

    Best Regards,

    Victoria

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Codernater Friday, March 3, 2017 4:02 PM
    Friday, March 3, 2017 9:45 AM
    Moderator

All replies

  • TO get the file properties, you have to load files first and then access its name.

    Like it's counterpart in REST api would be https://serverURL/_api/web/getFolderByServerRelativeUrl('<Folder server relative url>')/Files , it will give you files metadata, try same in PowerShell.

    • First load folder(which will be your doc lib) by server relative url.
    • Load folder.Files, which will give you file metadata.

    -Manjot


    • Edited by Manjot Singh1 Friday, March 3, 2017 5:08 AM
    • Proposed as answer by Manjot Singh1 Friday, March 3, 2017 5:27 AM
    • Unproposed as answer by Codernater Friday, March 3, 2017 4:02 PM
    Friday, March 3, 2017 5:07 AM
  • Hi,

    For getting $Item.File, we need to run ExecuteQuery() again for getting this object. So I have modified the foreach part as below.

    You can use the demo below:

    $User = "JohnSmith@SmithCorp.com"
    $SecureFile = "C:\Users\JohnSmith\OneDrive - Smith Corporation\Hotels\PowerShell\MyPassword.txt"
    $Password = Get-Content $SecureFile | ConvertTo-SecureString 
    
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
    $Site = "https://smithcorp.sharepoint.com/bif"
    $DocLib = "Documents" 
    
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site) 
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password) 
    $Context.Credentials = $Credentials
    
    $List = $Context.Web.Lists.GetByTitle($DocLib) 
    $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $Items = $List.GetItems($Query)
    
    $Context.Load($List)
    $Context.Load($Items) 
    $Context.ExecuteQuery()
    
    foreach($Item in $Items)
    {   
        $File=$Item.File 
        $Context.Load($File) 
        $Context.ExecuteQuery()
        Write-Host "Item ID="$Item.Id " | Item File Name="$File.Name
    }
    

    Best Regards,

    Victoria

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Codernater Friday, March 3, 2017 4:02 PM
    Friday, March 3, 2017 9:45 AM
    Moderator