SharePoint 2013 Search: JavaScript CSOM Primer | Microsoft
Microsoft Blog

SharePoint 2013 Search: JavaScript CSOM Primer

A lot of JavaScript incoming!

With the rise of SharePoint Online, custom search solutions through JavaScript have become a staple for the development work for clients. While a lot of your search basics can be handled through simple modification of Display Templates (see some of my previous blogs for some basics there), the opportunity still arises when you need to do something truly custom with your search code.

A lot of the basics are already out there for querying the Client Side Object Model through JavaScript (see this MSDN post by Cory Roth). We will review those, and take it a little deeper in order to give you a little more control over your queries and results returned.

We will cover:

  • The essentials and basics
  • Retrieving custom retrievable Managed Properties
  • Sorting by Managed Properties
  • Increase the Row Limit (results returned)
  • Setting a specific Result Source
  • Set Trim Duplicates to False

The Basics

The great part about all of the new 2013 Client Side tools is that you can just open up a Script Editor or a Content Editor Web Part and get started instantly. No need to deploy any managed code. If there’s items in your SharePoint search index and you have permission to access them, you can jump right in and get started.

Throw a div on the page with the ID “resultsDiv”, and see your table appear when you execute the JavaScript code. If you want to go deeper, simply add a button and a textbox to your page as well and replace the appropriate portions of the code.

To change the search query, just change the following line:

Retrieving Custom Managed Properties

In the request to the search index, there is a default set of managed properties that are returned by the index. In order to return other managed properties, we must request them.

Sorting by Managed Property

Sorting is fairly straightforward. Choose the Managed Property that you would like to sort by (make sure it’s set as Sortable!). Choose whether it needs to be Ascending or Descending. Ascending is 0, Descending is 1.

Increase the Number of Results Returned

The default number of results returned is 50 and is capped at 500. You can increase the cap through PowerShell.

Setting A Specific Result Source

You will need the GUID of the Result Source.

Trim Duplicates

In the potential rare instance that you will need to see duplicates, you can set Trim Duplicates to false.

Subscribe to the Microsoft Weekly Digest

* indicates required

One thought on “SharePoint 2013 Search: JavaScript CSOM Primer

  1. Pingback: SharePoint, Office365 & Yammer Nuggets of week 3 - Ragnar Heil: SharePoint Nuggets - Site Home - MSDN Blogs

Leave a Reply

Perficient Microsoft Blog

Insights, best practices and technical perspectives to help you leverage your investment in Microsoft technology solutions to power your business growth