Working With Search And Refiners Using CSOM PowerShell On SharePoint

In this article you will learn how to search data on SharePoint Online/ SharePoint 2013 using refiners with CSOM PowerShell.

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.

  1. 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.
    1. Add-Type -

      Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll", 

    2. 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",

    3. 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".

  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  3. 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. 
    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Not required for on premise site - End  

  4. 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.

  5. First we will identify the refinement data to be passed to get the final result set.

    1. Initialize the new Keyword query object.

    2. Set the query text and refiners to be applied.

    3. Get the results using SearchExecutor object and execute query method. Following piece of code will fetch us all the refinement filter type values.
      1. $searchquery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)  
      2. $searchquery.QueryText = "*"  
      3. $searchquery.Refiners = "FileType"  
      4. $searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)      
      5. $searchResults = $searchExec.ExecuteQuery($searchquery)      
      6. $ctx.ExecuteQuery()  

  6. Get the refinement results. Get the filter type value for ppt document items. Then build the refinement filter query.
    1. #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.  
    2. $refinementResults = $searchResults.Value[1]  
    3. $pptRefinement = ""  
    4. foreach ($refinementOption in $refinementResults.ResultRows)  
    5. {             
    6.     write-host "RefinerName : " $refinementOption.RefinementName "RefinementToken : " $refinementOption.RefinementToken         
    7.     #Gets refinement token value for powerpoint documents.  
    8.     if($refinementOption.RefinementName -contains "ppt"){  
    9.         $pptRefinement = $refinementOption.RefinerName + ":" + $refinementOption.RefinementToken              
    10.     }  
    11. }  

  7. Get the final result set using the refinement filters which we have obtained in the above step.

    1. Initialize the keyword query object with necessary query text and refiners.

    2. Then add the refinement filter data. (For example, FileType:"ppt refiner value")

    3. Execute the query using the executor object and initialized query.

    4. Finally Execute query using the context.

    5. Using for each loop, get the necessary results.
      1. $searchquery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)  
      2. $searchquery.QueryText = "your keyword"  
      3. $searchquery.Refiners = "FileType"  
      4.  
      5. #Add necessary refinement filters here. In my case, I have added only ppt filter query  
      6. $searchQuery.RefinementFilters.Add($pptRefinement)  
      7. $searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)      
      8. $searchResults = $searchExec.ExecuteQuery($searchquery)      
      9. $ctx.ExecuteQuery()  
      10.   
      11. foreach ($resultItem in $searchResults.Value[0].ResultRows)  
      12. {             
      13.     Write-Host $resultItem.Title  
      14. }  

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.