SP 2013: Searching in SharePoint 2013 using the REST new API’s

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren


Search has always been a great way to create custom solutions that aggregate and finds information in SharePoint. With SharePoint 2013, the search capabilities are heavily invested in and we see a lot of new ways to perform our searches. In this post I’ll show a simple example of how you can utilize the REST Search API’s in SharePoint 2013 to perform a search.

REST Search in SharePoint 2013

So in order to get started, we’ll need to have an ASPX Page containing some simple markup and a javascript file where we’ll put our functions to be executed. The approach mentioned in this post is also compatible with SharePoint Apps, should you decide to develop an App that relys on Search. In my example I’ve created a custom Page which loads my jQuery and JavaScript files, and I’m deploying those files using a Module to the SiteAssets library.

Preview of the simple solution


Creating a Search Application using REST Search Api’s

Let’s examine how you can construct your search queries using REST formatting and by simply changing the URL to take in the proper query strings.

Formatting the Url

By formatting the Url properly you can retrieve search results pretty easily using REST.

Query with querytext

If you simply want to return all results with no limits or filters from a search, shoot out this formatted url:


will yield the following result:


Essentially we’re getting a bunch of XML returned from the query which we then have to parse and handle somehow. We can then use the result of this query in our application in whatever fashion we want. Let’s see what a very simple application could look like, utilizing the SharePoint 2013 rest search api’s.

ASP.NET Markup Snippet

As we can see in the following code snippet, we’re simply loading a few jQuery and JavaScript files that we require and we define a Search Box and a Button for controlling our little Search application.

<!-- Load jQuery 1.8.3 -->
<script type="text/javascript" src="/SiteAssets/ScriptArtifacts/jquery-1.8.3.min.js"></script>

<!-- Load our custom Rest Search script file -->
<script type="text/javascript" src="/SiteAssets/ScriptArtifacts/RestSearch.js"></script>

<!-- Add a Text Box to use as a search box -->
<input type="text" value="Awesome" id="searchBox" />

<!-- Add a button that will execute the search -->
<input type="button" value="Search" onclick="executeSearch()" />

<div id="searchResults"></div>

JavaScript logic (RestSearch.js)

I’ve added a file to the project called RestSearch.js, which is a custom javascript file containing the following code which will perform an ajax request to SharePoint using the Search API’s:

// in reality we should put this inside our own namespace, but this is just a sample.
var context;
var web;
var user;

// Called from the ASPX Page
function executeSearch()
    var query = $("#searchBox").val();

    SPSearchResults =
        element: '',
        url: '',

        init: function(element) 
            SPSearchResults.element = element;
            SPSearchResults.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + query + "'";

        load: function() 
                    url: SPSearchResults.url,
                    method: "GET",
                            "accept": "application/json;odata=verbose",
                    success: SPSearchResults.onSuccess,
                    error: SPSearchResults.onError

        onSuccess: function (data)
            var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;

            var html = "<div class='results'>";
            for (var i = 0; i < results.length; i++)
                var d = new Date(results[i].Cells.results[8].Value);
                var currentDate = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes();

                html += "<div class='result-row' style='padding-bottom:5px; border-bottom: 1px solid #c0c0c0;'>";
                var clickableLink = "<a href='" + results[i].Cells.results[6].Value + "'>" + results[i].Cells.results[3].Value + "</a><br/><span>Type: " + results[i].Cells.results[17].Value  + "</span><br/><span>Modified: " + currentDate + "</span>";
                html += clickableLink;
                html += "</div>";

            html += "</div>";

        onError: function (err) {
            $("#searchResults").html("<h3>An error occured</h3><br/>" + JSON.stringify(err));

    // Call our Init-function

    // Call our Load-function which will post the actual query

The aforementioned script shows some simple javascript that will call the Search REST API (the "_api/…" part of the query) and then return the results in our html markup. Simple as that.


By utilizing the REST Search API we can very quickly and easily create an application that searches in SharePoint 2013.

This can be implemened in SharePoint Apps, Sandboxed Solutions or Farm Solutions. Whatever is your preference and requirements, the Search API’s should be easy enough to play around with.


Follow me

Tobias Zimmergren

Product Owner, Cloud Offerings at Rencore GmbH
Tobias Zimmergren delivers high-quality articles about business and technology around the Microsoft scene.

Tobias focuses on advisory and consultancy for the Office 365 and SharePoint offerings from Microsoft.
Follow me
  • Pingback: SharePoint 2013: REST Search - Life in DotNet()

  • mirontoli

    Awesome stuff! Thanks for sharing!

    • http://www.zimmergren.net/ Tobias Zimmergren


  • Rajesh Sitaraman

    Interesting one, I really like your way of presentation. Thanks for sharing.

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi Rajesh,

      Thank you for your kind words, I appreciate it.



  • Pingback: Best loved SharePoint posts: Week 24 Dec 2012()

  • http://www.cygnet-infotech.com/microsoft/sharepoint-solutions Steve- SharePoint Developer

    Worth reading thanks for the share!


    • http://www.zimmergren.net/ Tobias Zimmergren

      Thank you for your comment Steve.

  • http://twitter.com/sharepointapp SP Innovations

    thanks for this!

    • http://www.zimmergren.net/ Tobias Zimmergren


  • Drini

    Great Post! Do you know how to limit the search to a specific site collection?

  • Kourosh

    Hi, thanks, what about if I create a aspx page on my project completely on other domain?

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi Kourosh,
      I think you’ll have to elaborate your question a bit more :-)

  • Pingback: SharePoint Daily » Blog Archive » When SharePoint Governance Lacks Accountability; Business Isn’t Happy with SharePoint; Design Over Usability()

  • Pingback: When SharePoint Governance Lacks Accountability; Business Isn't Happy with SharePoint; Design Over Usability - SharePoint Daily - Bamboo Nation()

  • Kourosh Saleh

    Hi, I used this query to get all documents (among others) containing a word which starts with tes, however it does not any document at all. But when I enter tes* within the search box it returns all documents which starts with tes for example test2 or testing..
    here is my query._spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’tes*’

  • Ofer Gal

    How would you format a query to retrieve all the people who report to a given person?

    • http://www.zimmergren.net/ Tobias Zimmergren

      Depends on how you want to achieve it. Through user profile properties, through search (e.g. if you have an employment directory that is searchable) etc. I’ve done a similar solution, where the information has been stored in the user profile properties, and just executed a CSOM/REST call to fetch the information from the profiles.

      More on CSOM and User Profiles can be found here: http://msdn.microsoft.com/en-us/library/office/jj920104.aspx


  • Swati Sumi

    Thanks for sharing..But does it work for special character as well..

    If the query text contains special character it throws an error
    “The query string “querytext” is missing or invalid.”.

    I tried encoding it..but still no luck.
    Any clue how to fix it?

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi Swati,
      Can you please elaborate what your query string looks like, and what the special characters are that doesn’t work?


      • Swati Sumi

        it doestnt work with # ,& and many other symbol.
        Query is as following
        var Department = “some text with special characters”
        querystring = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’Department:” + Department + “‘;

        • Swati Sumi

          I have done a Quick fix by replacing these characters with respective encoded value..but I am looking for a generic solution which works fine with all special characters.

          • http://www.zimmergren.net/ Tobias Zimmergren

            Hi Swati,
            Well if it’s in your query string in javascript, I’m pretty sure you’d need to escape the characters as they might otherwise break the actual HTML and script markup.

            You could of course create a generic function for replacing special characters automatically, but I think that’s on you and not SharePoint.

          • Alex Xie

            Regarding the special characters, how to handle the double quote symbol ” in REST-based web services? Will ” be treated as noise word and ignored or will be treated a word in SP2013 search?

            I have a search application for company stock products search. There are a lot of product descriptions which contains ” and -, such as 1/2″, 3/4, 1-1/2″ Hex Cap Screw. How can I config the search to return the correct search results?

  • Dilan

    Is there any way to set up scope for results, let say I want to get results only from particular web or from particular list. And also can it use for search taxonomies, normally api returns GUID for Taxonomies

  • Pingback: SharePoint 2013 Search REST API | Share your knowledge()

  • Vik

    Hi Tobias,

    Can use the same thing in the HTML page out side the SharePoint Farm.

    Just to give background we are planning to replace 3rd party search tool with the sharepoint fast search and planning to use the rest api to show the result on the html application.

  • SaMol PP

    hi Tobias,
    thanks for the article.
    i have a requirement in which i need to implement search from the sub site ONLY. this sub site is being passed as a query string and in the search center site collection page there is a search box web part with button , when i enter the keywords and click on search i need to pass this sub site as a parameter along wit the keyword. and i need to show the results in the OOTB core results search web part.
    Can you help me here , how to proceed with this? should i implement search api here?
    since the sub site name is dynamic, any option of using/leveraging OOTB searchw eb parts in sp 2013 is ruled out?
    help is highly appreciated!

  • Bala Murugan S


    I need to fetch sharepoint search result and show it on PHP application . for that i am using REST API. Unfortunely i am facing below error. I noticed that this is because of permission. How to resolve . Please help me .

    {“readyState”:0,”status”:0,”statusText”:”No Transport”}

    • http://www.zimmergren.net/ Tobias Zimmergren

      Hi Bala,

      I think that question is best suited for the official MSDN Forums: http://www.mssharepointforums.com


      • Manoj

        Hi Tobias,

        I got the same error which Bala mentioned above. And you have also provided one link but in that link we could not find the solution. So please can you provide the exact solution url.


      • Manoj

        Hi Tobias,

        Please let me know how to set proxy in your code given above. Because I am not able to access that url directly. That is why need to set proxy.


        • http://www.zimmergren.net/ Tobias Zimmergren

          Hi Manoj,

          Please refer to the MSDN or StackExchange forums for that mate. I’m sure there’s plenty of people to help out who may have more time on their hands than I currently do.

          I hope you get it resolved!


  • Sam Lam

    Hi, I try to create a html page and using your code, once the script is run, then the error is shown: {“readyState”:0,”status”:0,”statusText”:”No Transport”}
    does the script run in SharePoint page?

  • Marko Gordić


    Is it possible to include user profile properties as well in query? Like Department?

    I am trying to fetch tasks and I am getting values from task item, but all user profile properties are null in search result (although there are value if you search for an user)

  • rahul majumdar


    I have been using your potential code snippet to use search functionality.
    Currently i need to search the documents inside one single site but this api returns the search result for across all the sites.

    Is there any workaround for this?

  • niki

    Hi Tobias,

    We have been using search rest query extensively. No the issue is with the help of rest query we are fetching publishing content of appx. 200 results. And content being large enough is making Post data heavy enough. So, is there any way so that we can get particular characters of data in search results. .

    Thanks in advance.