Wednesday, March 26, 2014

RESTful way to retrieve the Search Center URL

On one of my daily Fiddler adventures I stumbled upon a REST end-point I hadn’t seen before in SharePoint Online (Does not currently work on-premises with 2013 sp1, but hoping it will arrive in an update).

/_api/search/searchcenterurl

The end-point is not documented or mentioned on MSDN, but is easy to use. You call it with a GET and get something like the following back in JSON

{"d":{"searchcenterurl":https://contoso.sharepoint.com/search/Pages}}

If you’re using jQuery you can get the search center url for the current site using the following code:

jQuery.ajax({
url: _spPageContextInfo.webServerRelativeUrl+"/_api/search/searchcenterurl",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
})
.done(function (data) {
alert(data.d.searchcenterurl);
});


A use case could be that you have a page with some tags displayed, and when clicking on the tags you want to execute a search from the search center on the clicked tag. By tacking on Results.aspx?k=<your query> to the returned URL you can easily redirect the user to the result page of your choice with a predefined query.