Applies to: SharePoint List Filter Web Part for SharePoint on premise (SP2010, SP2013, SP2016, SP2019)
Recently a customer asked us how to add the Filter Web Part to the default view for all lists. This can be done by using a simple SharePoint Powershell Script, which you can find below.
#open the web $web = Get-SPWeb http://yoursite #load the required assemblies $fwpAssembly = [System.Reflection.Assembly]::LoadWithPartialName("sapiens.at.SharePoint.FilterWebPart") $frameworkAssembly = [System.Reflection.Assembly]::LoadWithPartialName("sapiens.at.SharePoint") ForEach($l in $web.Lists){ try{ #open the web part manager of the default view $wpm = $web.GetLimitedWebPartManager($l.DefaultView.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared); #check if there is a XsltListViewWebPart on the view page and no FilterWebPart $lvwps = $wpm.WebParts | where { $_.GetType().FullName -eq "Microsoft.SharePoint.WebPartPages.XsltListViewWebPart" } $fwps = $wpm.WebParts | where { $_.GetType().FullName -eq "sapiens.at.SharePoint.FilterWebPart.FilterWebPart" } if($lvwps.Count -eq 1 -and $fwps.Count -eq 0){ #new FilterWebPart $fwp = New-Object sapiens.at.SharePoint.FilterWebPart.FilterWebPart #connect with ListViewWebPart $fwp.ConnectedWebPartId = $lvwps[0].StorageKey.ToString(); #configure the search fields (all list fields that can be used in the filter web part) $fwp.SearchFieldsString = (($l.Fields | where { try{ [Sapiens.at.SharePoint.FilterFields.FilterField]::IsFieldSearchable($_) }catch { } } | foreach {$_.InternalName}) -join ';#'); #complete configuration $fwp.IsConfigured=$true; #Add the filter web part to the view page $newWP = $wpm.AddWebPart($fwp, "Main", 0); write-host ("Web part added to " + $l.DefaultViewUrl) } } catch [exception] { write-host ("An error occurred when adding web part to " + $l.DefaultViewUrl + ": " + $_.Exception.Message) } }
#open the web $web = Get-SPWeb http://yoursite #load the required assemblies $fwpAssembly = [System.Reflection.Assembly]::LoadWithPartialName("sapiens.at.SharePoint.FilterWebPart") $frameworkAssembly = [System.Reflection.Assembly]::LoadWithPartialName("sapiens.at.SharePoint") ForEach($l in $web.Lists){ try{ #open the web part manager of the default view $wpm = $web.GetLimitedWebPartManager($l.DefaultView.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared); #check if there is a XsltListViewWebPart on the view page and no FilterWebPart $lvwps = $wpm.WebParts | where { $_.GetType().FullName -eq "Microsoft.SharePoint.WebPartPages.XsltListViewWebPart" } $fwps = $wpm.WebParts | where { $_.GetType().FullName -eq "sapiens.at.SharePoint.FilterWebPart.FilterWebPart" } if($lvwps.Count -eq 1 -and $fwps.Count -eq 0){ #new FilterWebPart $fwp = New-Object sapiens.at.SharePoint.FilterWebPart.FilterWebPart #connect with ListViewWebPart $fwp.ConnectedWebPartId = $lvwps[0].StorageKey.ToString(); #configure the search fields (all list fields that can be used in the filter web part) $fwp.SearchFieldsString = (($l.Fields | where { try{ [Sapiens.at.SharePoint.FilterFields.FilterField]::IsFieldSearchable($_) }catch { } } | foreach {$_.InternalName}) -join ';#'); #complete configuration $fwp.IsConfigured=$true; #Add the filter web part to the view page $newWP = $wpm.AddWebPart($fwp, "Main", 0); write-host ("Web part added to " + $l.DefaultViewUrl) } } catch [exception] { write-host ("An error occurred when adding web part to " + $l.DefaultViewUrl + ": " + $_.Exception.Message) } }
This script opens the Web Part manager of the default view and checks whether there is an XsltListViewWebPart and FilterWebPart. If it finds an XsltListViewWebPart and no FilterWebPart, it adds a new FilterWebPart and connects it with the ListViewWebPart. It then configures the search fields used in the Filter Web Part and adds it to the view page.
The Web Part is configured so that all list fields can be searched using the keyword search. Filter details (for refinement) are not configured. For more configuration possibilities, see the following blog post on how to configure the List Filter Web Part programmatically.
I hope you found this information useful. If you have any problems or any other questions, please send an e-mail to support@sapiens.at or post a comment.