SharePoint Online:使用 PowerShell 筛选列表视图以仅显示子文件夹中的文件
要求:设置列表视图筛选器以显示 SharePoint Online 文档库中特定文件夹中的文件。
如何设置视图过滤器以显示文件夹中的文件?
不幸的是,SharePoint Web 用户界面不允许我们设置基于文件夹或子文件夹的视图过滤器。虽然可以通过编辑 XSLT-CAML 代码来设置列表视图筛选器,以使用 SharePoint Designer 仅显示特定子文件夹中的文件,但以下是我在 SharePoint Online 列表视图上设置筛选器的 PowerShell 方法。
SharePoint Online:PowerShell 创建按文件夹筛选的列表视图
此 PowerShell 脚本创建一个列表视图,显示 SharePoint Online 文档库中特定文件夹中的所有文件和文件夹。
#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
更喜欢元数据而不是文件夹的另一个原因!
