How to implement search query in csom powershell

Sayantan Ganguly 0 Reputation points

HI Team,

I have one script where I am trying to get ViewsLifeTime and ViewsRecent using Search querying CSOM PowerShell. I am getting error: New-Object : Cannot find type [Microsoft.SharePoint.Client.Search.Query.KeywordQuery]: verify that the assembly containing this type is loaded.

Please help me to fix the code or else give me an example on how to implement search query using CSOM PowerShell.

My code is looking like below:

# Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"

# SharePoint Online site URL
$siteUrl = ""

# Credentials for SharePoint Online
$credential = Get-Credential

# Query keyword (e.g., page URL or GUID)
$queryKeyword = ""

# Initialize SharePoint client context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)

# Build the keyword query
$keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
$keywordQuery.QueryText = "Path:$siteUrl AND ($queryKeyword)"

# Execute the query
$searchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
$searchResults = $searchExecutor.ExecuteQuery($keywordQuery)

# Check if results are found
if ($searchResults.Value[0].ResultRows.Count -gt 0) {
    $viewsLifeTime = $searchResults.Value[0].ResultRows[0]["ViewsLifeTime"]
    Write-Host "ViewsLifeTime property value: $viewsLifeTime"
} else {
    Write-Host "Page not found or ViewsLifeTime property not available for the specified query."

# Dispose of the SharePoint client context

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,261 questions
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
1,314 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 16,741 Reputation points Microsoft Vendor

    Hi @Sayantan Ganguly ,

    To use the search query in CSOM PowerShell, you need to load the Microsoft.SharePoint.Client.Search.dll assembly. The error you are seeing indicates that the assembly is not loaded. You can load the assembly using the Add-Type cmdlet as shown below:

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Search.dll"

    In other words, add the above statement to the code you provide.

    User's image

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.