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 provide us an option to set view filter based on folder, sub-folders. While its 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 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
#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!
SharePoint Online: Filter List View to Show Files Only from a Sub-Folder using PowerShell SharePoint Online: Filter List View to Show Files Only from a Sub-Folder using PowerShell Reviewed by Salaudeen Rajack on August 13, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.