SharePoint Online: Filter List View to Show Files Only from a Sub-Folder using PowerShell

Requirement: Set a list view filter to show files from a specific folder in a SharePoint Online document library.

How to Set the View Filter to Show Files from a Folder?

Unfortunately, The SharePoint web user interface doesn’t allow us to set a view filter based on folders or sub-folders. While it is possible to set list view filter by editing the XSLT-CAML code to show files only from a specific sub-folder using SharePoint Designer, Here is my PowerShell way to set the filter on a SharePoint Online list view.

filter list view to show items from a folder in sharepoint

SharePoint Online: PowerShell to Create a List View Filtered by a Folder

This PowerShell script creates a list view that displays all files and folders from a specific folder in the SharePoint Online 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"
 
#Function to Create List view in SharePoint Online
Function Create-SPOListView()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ViewName,
        [Parameter(Mandatory=$true)] [String[]] $ViewFields,
        [Parameter(Mandatory=$true)] [string] $ViewQuery,
        [Parameter(Mandatory=$false)] [int] $ItemLimit = 30,
        [Parameter(Mandatory=$false)] [Bool] $IsDefaultView= $False
    )
 
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #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 List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
 
        #Check if the View exists in list already
        $ViewColl=$List.Views
        $Ctx.Load($ViewColl)
        $Ctx.ExecuteQuery()
        $NewView = $ViewColl | where { ($_.Title -eq $ViewName) }
        if($NewView -ne $NULL)  
        {
            Write-host "View '$ViewName' already exists in the List!" -f Yellow
        }
        else
        {
            $ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
            $ViewCreationInfo.Title = $ViewName
            $ViewCreationInfo.Query = $ViewQuery
            
            $ViewCreationInfo.RowLimit = $ItemLimit
            $ViewCreationInfo.ViewFields = $Viewfields
            $ViewCreationInfo.SetAsDefaultView = $IsDefaultView
 
            #sharepoint online powershell create view
            $NewView =$List.Views.Add($ViewCreationInfo)
            #Set the Scope
            $NewView.Scope = [Microsoft.SharePoint.Client.ViewScope]::RecursiveAll
            $NewView.Update()
            $Ctx.ExecuteQuery()    
             
            Write-host "New List View Created Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
} 
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName="Documents"
$ViewName="Active Documents"
$FolderRelativeURL = "/sites/marketing/Shared Documents/2018"
$ViewFields=@("DocIcon","LinkFilename","Modified","Editor") # Default Fields
$ViewQuery="<Where><Eq><FieldRef Name='FileDirRef'/><Value Type='Text'>$FolderRelativeURL</Value></Eq></Where>"
 
#Call the function to Create new View in the list
Create-SPOListView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName -ViewFields $ViewFields -ViewQuery $ViewQuery

Another reason to prefer metadata over folders!

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

3 thoughts on “SharePoint Online: Filter List View to Show Files Only from a Sub-Folder using PowerShell

  • hello, can i create a view on a sharepoint list, depending on a if they are or not member of a office 365 group?

    Thanks

    Reply
  • is there any way i create a view to see all the files and sub folders of sub folders of a specific sub folder

    Reply
    • Set the “$FolderRelativeURL” variable to the specific sub folder.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *