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:
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:
References:
Also, you can check few articles on:
- How to use Deferred and Promise in JavaScript Object Model in SharePoint 2013 to make asynchronous to synchronous call?
- Three State Workflow in SharePoint with Example.
- How to retrieve host web site title in add-in web in SharePoint hosted add-in in SharePoint Online Office 365 Site?
Conclusions:
Here we have discussed how we can set search center url in SharePoint Online Office 365 site collection using JSOM.