I am trying to run the following line:
$query = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
and receive this error:
New-Object : Cannot find an overload for "KeywordQuery" and the argument count: "1".
1. I have added paths to SDK:
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\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"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Search.dll"
Full code:
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$ctx.Load($ctx.Web)
$ctx.Load($ctx.Site)
$errorMessage=""
try
{
$ctx.ExecuteQuery()
}
catch [Net.WebException]
{
$errorMessage=$_.Exception.ToString()
}
Write-Host $ctx.Web.Url
$query = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
2. The library is correct and sample code works in C# in Visual Studio:
static ClientContext ctx;
static void Main(string[] args)
{
Credentials();
ctx.Credentials = new SharePointOnlineCredentials(userName, ss);
DoSomethingSearchish();
}
static void DoSomethingSearchish()
{
KeywordQuery kQ = new KeywordQuery(ctx);
kQ.QueryText = "prank";
SearchExecutor searchExecutor = new SearchExecutor(ctx);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(kQ);
ctx.ExecuteQuery();
foreach (var resultRow in results.Value[0].ResultRows)
{
Console.WriteLine(resultRow["Title"]);
}
Console.ReadKey();
}
The Visual Studio code was originally copied from somewhere, altered and used by me, but I cannot find the source now - apologies to the author.
3. In Powershell, using different number of arguments for the constructor gives the following errors:
0 - ...Query.KeywordQuery
A constructor was not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.Client.Search.Query.KeywordQuery
2 - ...Query.KeywordQuery($ctx, "")
New-Object : Cannot find an overload for "KeywordQuery" and the argument count: "2".
4. I am NOT running the code from SharePoint Online Management Shell (it is installed though) and I am NOT connected with Connect-SPOService.
Question: Why Powershell cannot find the constructor?