Did you ever used the search query in Powershell? Powershell can send a search query against the SharePoint Search. It is great in order to automate some tasks, in which you need data from various site collection which can be collected by simply using the search. So how would it look like?
Example
$SearchQuery = "ManagedProperty:Value"
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($clientContext)
$keywordQuery.QueryText = $SearchQuery
$keywordQuery.RowLimit = 500;
$searchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($clientContext)
$results = $searchExecutor.ExecuteQuery($keywordQuery)
$clientContext.ExecuteQuery()
$results.Value[0].ResultRows
3 things you should pay attention
So that is easy. But you should pay attention to three things if you use a Powershell script in order to make a search query.
#1 Choose the right user which executes the script
It is really important. The search results are based on the permission on the user who sends the request. So if you have a employee who would search for “secret documents” he would find less than if your SharePoint Search Account or Admin Account would do the same query. So if you automate tasks with it keep in mind that the user who runs the query should have correct permissions. Not too much but not too less.
#2 RowLimit
If you get always 50 search results it might be hint, that there is a limit. And that’s it. By default the search results row limit is set to 50 elements. You can raise it up to 500.
#3 Check your results
You should always check your results. Is the numbers of result rows correct and is the content of your search results correct? Could it be that there is an information which should not be due to permissions which are not correctly set? Better check twice
Resources
If you need a complete step by step instruction, here is a good how to guide
Leave a Reply