Introduction
In this article, you will learn how to search with refiners and query on SharePoint 2013 / Online sites using PowerShell CSOM.
Steps Involved
We will see how we can filter out the search results using filter type. The following steps explain the flow.
- Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.
- Add-Type -
Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll",
- Add-Type -
Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll",
- Add-Type
Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Search\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Search.dll".
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
- #Not required for on premise site - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Not required for on premise site - End
- If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.
- First we will identify the refinement data to be passed to get the final result set.
- Initialize the new Keyword query object.
- Set the query text and refiners to be applied.
- Get the results using SearchExecutor object and execute query method. Following piece of code will fetch us all the refinement filter type values.
- $searchquery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
- $searchquery.QueryText = "*"
- $searchquery.Refiners = "FileType"
- $searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
- $searchResults = $searchExec.ExecuteQuery($searchquery)
- $ctx.ExecuteQuery()
- Get the refinement results. Get the filter type value for ppt document items. Then build the refinement filter query.
- #Gets all the refinement results. Value[0] will fetch us the search result set, which we are not interested now as its not filtered data.
- $refinementResults = $searchResults.Value[1]
- $pptRefinement = ""
- foreach ($refinementOption in $refinementResults.ResultRows)
- {
- write-host "RefinerName : " $refinementOption.RefinementName "RefinementToken : " $refinementOption.RefinementToken
- #Gets refinement token value for powerpoint documents.
- if($refinementOption.RefinementName -contains "ppt"){
- $pptRefinement = $refinementOption.RefinerName + ":" + $refinementOption.RefinementToken
- }
- }
- Get the final result set using the refinement filters which we have obtained in the above step.
- Initialize the keyword query object with necessary query text and refiners.
- Then add the refinement filter data. (For example, FileType:"ppt refiner value")
- Execute the query using the executor object and initialized query.
- Finally Execute query using the context.
- Using for each loop, get the necessary results.
- $searchquery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
- $searchquery.QueryText = "your keyword"
- $searchquery.Refiners = "FileType"
-
- #Add necessary refinement filters here. In my case, I have added only ppt filter query
- $searchQuery.RefinementFilters.Add($pptRefinement)
- $searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
- $searchResults = $searchExec.ExecuteQuery($searchquery)
- $ctx.ExecuteQuery()
-
- foreach ($resultItem in $searchResults.Value[0].ResultRows)
- {
- Write-Host $resultItem.Title
- }
Like wise other properties can also be retrieved.
Summary
Thus you have learned how to access the search results using the refiners with CSOM PowerShell commands on SharePoint 2013/ SharePoint online.