in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

How to: Query SharePoint Search with PowerShell

I’ve been doing some content analysis with PowerShell lately and I thought it would be useful if I could query SharePoint search.  With the addition of Search to the Managed Client Object Model this task is pretty easily.  I have already written a post on how to do this in a console application.  Let’s adapt that code for use in PowerShell.

Create a new script using your favorite script editing tool such as PowerShell ISE.  We’ll call mine SearchQuery.ps1.  The first thing we want to do in this script is add a parameter so that the search query can be passed via command line.  We’re going to give this parameter a name of $queryText.  The second parameter is a path to a site collection.  We need this for our ClientContext object.

Param([string] $queryText, [string] $siteCollectionUrl)

Now we need to load the following assemblies like we did with our console application.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
  • Microsoft.SharePoint.Client.Search.dll

To do this in PowerShell, we use the following statements:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")

[

System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

[

System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Search")

Now, we get a reference to a ClientContext object to handle all of our client side requests.  W

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteCollectionUrl)

Once we have this object, we can use it to get a KeywordQuery object.

$keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($clientContext)

We then use the QueryText property to specify our search query.  In this case, we are just passing the value of $queryText received from the command line.

$keywordQuery.QueryText = $queryText

If you haven’t used PowerShell ISE, it will also provide you IntelliSense on your objects similar to Visual Studio.

PowerShellISEIntellisense

Now, we need a SearchExecutor class to execute our query.  It requires a ClientContext as well.

$searchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($clientContext)

To execute the query, we use the ExecuteQuery method.  It will return us a $results object which we will access in a similar manner as we did with our client application.  Pass it our KeywordQuery object.

$results = $searchExecutor.ExecuteQuery($keywordQuery)

This doesn’t execute the query though.  Since this is a client object model, we still need to call ExecuteQuery() on our ClientContext object to make the query happen.

$clientContext.ExecuteQuery()

Just like in other examples, the individual results are buried in $results.Value[0].ResultRows.  What’s also unfortunate is that each row comes back with a series of Key / Value pairs which makes binding this data in bulk somewhat painful.  We can example the object and format it in a table but it doesn’t give us much to work with.

$results.Value[0].ResultRows

PowerShellSearchResultsNoFormat

If you know exactly what fields you are looking to access though you can just iterate through the results with a foreach statement.

foreach($result in $results.Value[0].ResultRows)

{

Write-Host $result["Title"] Write-Host $result["Path"] Write-Host $result["Write"]

Write-Host

}

It’s not ideal, but it works alright for small batches of results.  Here’s what my results look like now.

PowerShellSearchResultsSomeFormat

This gives us some search results that we can work with.  Now you use them in PowerShell as you need.  It should even work on remote servers provided the user account you are running under has permissions to the SharePoint site.

Comments

 

How to: Query SharePoint Search with PowerShell... said:

Pingback from  How to: Query SharePoint Search with PowerShell...

January 13, 2014 3:18 PM
 

How to: Query SharePoint Search with PowerShell... said:

Pingback from  How to: Query SharePoint Search with PowerShell...

January 14, 2014 3:50 AM
 

Jimmy said:

Awesome work. I am trying a similar route to MMS Taxonomy, but no success. Maybe this can be your next challenge.

January 23, 2014 6:40 AM
 

Blog de Juan Carlos González en Geeks.MS said:

Como estrenamos mes nuevo, toca un nuevo recopilatorio de enlaces sobre SharePoint 2013. Como siempre

February 1, 2014 6:08 AM
 

SharePoint 2013: Recopilatorio de enlaces interesantes (XXIX)! | Pasi??n por la tecnolog??a... said:

Pingback from  SharePoint 2013: Recopilatorio de enlaces interesantes (XXIX)! | Pasi??n por la tecnolog??a...

February 1, 2014 6:08 AM
 

The Microsoft MVP Award Program Blog said:

1. How to: Query SharePoint Search with PowerShell By SharePoint MVP Corey Roth 2. Creating Reports

February 2, 2014 2:13 PM
 

Friday Five – January 30, 2014 | MSDN Blogs said:

Pingback from  Friday Five  – January 30, 2014 | MSDN Blogs

February 3, 2014 4:59 AM
 

Friday Five – January 30, 2014 | MSDN Blogs said:

Pingback from  Friday Five  – January 30, 2014 | MSDN Blogs

February 3, 2014 5:59 AM
 

Friday Five – January 30, 2014 | MSDN Blogs said:

Pingback from  Friday Five  – January 30, 2014 | MSDN Blogs

February 3, 2014 6:59 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems