Pinning Fields to the Filter Pane in SharePoint Libraries Using PowerShell
Introduction
In SharePoint Online, the filter pane is a powerful feature that allows users to quickly filter and find relevant data in libraries and lists. However, by default, not all fields are visible in the filter pane. To enhance usability, you can pin specific fields to the top of the filter pane using PowerShell or the REST API.
This blog post demonstrates how to use PowerShell and the REST API to pin fields to the filter pane in SharePoint libraries.
Using the REST API
The SharePoint REST API provides a way to update field properties, including the ShowInFiltersPane
property, which controls whether a field is pinned to the filter pane.
Example REST API Request
To pin a field to the filter pane, you can use the following REST API request:
Endpoint:
Endpoint Url = https://contoso.sharepoint.com/sites/testsite/_api/web/GetList('/sites/testsite/PO')/Fields('081c6e4c-5c14-4f20-b23e-1a71ceb6a67c')
Method: POST
Payload= ‘{"__metadata":{“type”:“SP.Field”},“ShowInFiltersPane”:1}’
This request updates the ShowInFiltersPane property of the specified field, making it visible in the filter pane.
Using PowerShell to Pin Fields
For a more automated approach, you can use PowerShell with the PnP PowerShell module to pin fields to the filter pane. Below is a script that demonstrates how to achieve this.
PowerShell Script
# This script updates a field within a SharePoint Online library to pin it to the top of the filters pane.
# Parameters
function Pin-FieldsInList() {
param (
[string]$SiteUrl,
[string]$ListTitle,
[string[]]$FieldNames
)
# Connect to SharePoint Online
Connect-PnPOnline -Url $SiteUrl
# Loop through each field to update its properties
foreach ($FieldName in $FieldNames) {
Write-Host "Updating field '$FieldName' in library '$ListTitle'..."
# Get the field
$Field = Get-PnPField -List $ListTitle -Identity $FieldName
if ($Field) {
# Update the field to show in the filters pane
Set-PnPField -List $ListTitle -Identity $FieldName -Values @{ShowInFiltersPane = 1}
Write-Host "Field '$FieldName' has been pinned to the filters pane."
} else {
Write-Host "Field '$FieldName' not found in library '$ListTitle'."
}
}
}
# Example how to call the function Pin-FieldsInList
#Pin-FieldsInList -SiteUrl "https://contoso.sharepoint.com/teams/TestMultipleLibraries" -ListTitle "amberlib" -FieldNames @("Modified", "Type")
Explanation of the Script
1.Parameters:
SiteUrl: The URL of the SharePoint site. ListTitle: The name of the library or list where the fields are located. FieldNames: An array of field names to pin to the filter pane. Connect to SharePoint Online:
2. Connect to SharePoint Online:
The script uses Connect-PnPOnline to authenticate and connect to the SharePoint site. Loop Through Fields:
3. Loop Through Fields:
For each field in the FieldNames array, the script retrieves the field using Get-PnPField. If the field exists, it updates the ShowInFiltersPane property using Set-PnPField.
4.Output:
The script provides feedback in the console, indicating whether each field was successfully updated or not found.
Conclusion
Customizing the filter pane in SharePoint Online can greatly improve the user experience by making important fields easily accessible.
By automating this process with PowerShell, you can efficiently manage multiple libraries and lists, saving time and ensuring consistency across your SharePoint environment.