Part 1 blog discuss about creation of view filter for Modern SharePoint site. This part we discuss how to create View filter based on Metadata field for Classic SharePoint site.
Classic SharePoint Site – How to Create View Filter
In my scenario I have a document library “Documents” which has several type of documents with the category. The category is the Metadata / Taxonomy field. The Category has EU as top level TermSet, below is the screenshot of the termset and terms.
Step – 1:
Create Document Library with the Taxonomy / Metadata field. In my case Category column with EU termset with all the terms. The document library has several folders and documents inside tagged with the metadata needed.
Step – 2:
Create a view to show all the documents without folder. Name the view as “O365 Files” the name can be any meaningful name which you can create. While creating the view make sure you select “Show all items without Folders” under Folders. In my case I want to see all documents uploaded to library without any folders. This view filter will be modified later by the PowerShell CSOM script.
Below is the view I created with the name O365 Files. This view will be changed using the PowerShell script to update the filtering metadata.
Step – 3
Identify the Metadata tag ID you want to filter. The metadata tag id is the hidden id which is created under the site collection to store the data. There will be a hidden list under site collection. Make sure you have atleast one document uploaded and tagged with the metadata you wanted to filter. The Metadata will have a unique Id in the hidden list. Navigate to https://<sitecollection>/Lists/TaxonomyHiddenList/AllItems.aspx to get the list. I have created a new view for this list to see the Id column easily called ShowId. Below is the screenshot of the List with the Id column made visible. To filter the documents by O365 and all its children metadata note down all the Ids. In my case below are the ids
Step – 4:
Prepare to run the PowerShell CSOM script to update the View CAML. Download the CSOM dll from Nuget.org https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/16.1.7414.1200 Use the Manual download link at the right side of the page. The downloaded file will be microsoft.sharepointonline.csom.16.1.7414.1200.nupkg based on the version you download. Unzip the file to the folder. You can change the file extension to .zip and unzip all files. Use the .net45 folder libraries for the CSOM PowerShell.
Step – 5:
Add-Type -Path “C:\Documents\PS\CSOM\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Documents\PS\CSOM\Microsoft.SharePoint.Client.Runtime.dll”
$username = "<USERNAME>" $password = "<PWD>" $securePassword = ConvertTo-SecureString $password -AsPlainText -Force $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) Write-Host "Connecting to site ..." $srcUrl = "<SITE URL>" $srcContext = New-Object Microsoft.SharePoint.Client.ClientContext($srcUrl) $srcContext.Credentials = $credentials $srcWeb = $srcContext.Web $srcList = $srcWeb.Lists.GetByTitle("Documents") $view = $srcList.Views.GetByTitle("O365 Files") $srcContext.Load($srcWeb) $srcContext.Load($srcList) $srcContext.Load($view) $srcContext.ExecuteQuery() Write-Host "Connected successfully" $query = @" <OrderBy><FieldRef Name="Modified" Ascending="FALSE" /></OrderBy> <Where><And><Geq><FieldRef Name="Modified" /><Value Type="DateTime"><Today OffsetDays="-30" /></Value></Geq> <In> <FieldRef LookupId="TRUE" Name="Category" /> <Values> <Value Type="Integer">37</Value> <Value Type="Integer">40</Value> <Value Type="Integer">53</Value> <Value Type="Integer">54</Value> <Value Type="Integer">55</Value> <Value Type="Integer">56</Value> <Value Type="Integer">57</Value> </Values> </In> </And></Where> "@ if($view) { #Write-Host $view.ViewQuery -ForegroundColor Yellow #$view.Scope = 1 $view.ViewQuery = $query $view.Update() $srcContext.Load($view) $srcContext.ExecuteQuery() Write-Host "------------------------Updated---------------------" } else { Write-Host "NULL" } Write-Host "Done"