How to implement search query in csom powershell

Sayantan Ganguly 80 Reputation points
2023-09-08T16:00:03.2133333+00:00

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 = "https://domain.sharepoint.com/sites/sitename"

# Credentials for SharePoint Online
$credential = Get-Credential

# Query keyword (e.g., page URL or GUID)
$queryKeyword = "https://wvbss.sharepoint.com/sites/SayantanMainSite/SitePages/Home.aspx"

# 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
$ctx.Dispose()

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,949 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,469 questions
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 25,626 Reputation points Microsoft Vendor
    2023-09-11T08:58:35.2866667+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.