Share via


SharePoint Online: How to set Search Center URL using JSOM

Introduction:

In this post, we will discuss how we can set search center URL in SharePoint Online Office 365 site collection using JavaScript object model code. The same code will also work in SharePoint 2016 or SharePoint 2013.

To change the search center URL using browser we can navigate to Site Settings -> then click on "Search Settings" which is under "Site Collection Administration".

By default it will be blank like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/sharepoint_2013_change_search_center_url.png

This we can set using Jsom also.

Here let us take a button and on click of that button, we will change the search center url. Both the html and jsom code we have written inside a script editor web part which is inside a web part page.

We can set the SRCH_ENH_FTR_URL_SITE property which will change the search center url.

Code:

<input type="button" id="btnSubmit" value="Set Search URL" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function () {
    bindButtonClick();
});
function bindButtonClick() {
    $("#btnSubmit").on("click", function () {
        setSearchURL();
    });
}
  
function setSearchURL() {
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_site().get_rootWeb();
var props =  web.get_allProperties();
props.set_item("SRCH_ENH_FTR_URL_SITE","/sites/Bhawana/MySearchCenter");
web.update();    
ctx.load(web);    
ctx.executeQueryAsync(function () {
    alert("Search Settings Modified");
 },
 function() {
  alert("failed");
});
}
</script>

Once you Save the code and click on the button, it will set the search center url like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/sharepoint_2013_change_search_center_url_jsom.png

References:

Also, you can check few articles on:

Conclusions:

Here we have discussed how we can set search center url in SharePoint Online Office 365 site collection using JSOM.