4
votes

In SharePoint 2013, I am trying to access Search object through JavaScript CSOM.

I want to know the object which can give me the access to Search Settings under Site Settings. I tried looking under SP object but I didn't find any Search related object there.

My goal is to change the search Center URL through JavaScript CSOM.

Thanks in Advance!!!

2

2 Answers

1
votes

How to set Search Settings in SharePoint 2013 via CSOM

 function updateSearchSettings(searchSenterUrl,resultsPageUrl,Success,Error) {
   var context = SP.ClientContext.get_current();
   var web = context.get_site().get_rootWeb();
   var props =  web.get_allProperties();

   props.set_item("SRCH_ENH_FTR_URL_SITE",searchSenterUrl);
   props.set_item("SRCH_SB_SET_SITE",JSON.stringify({"Inherit":false,"ResultsPageAddress":resultsPageUrl,"ShowNavigation":false}));
   web.update();

   context.load(props);
   context.executeQueryAsync(
     function () {
        var searchCenterUrl = props.get_item("SRCH_ENH_FTR_URL_SITE");
        var searchPageProps = JSON.parse(props.get_item("SRCH_SB_SET_SITE"));
        Success(searchCenterUrl,searchPageProps);
     },
     Error
   );
}


//Usage
updateSearchSettings("/sites/search/pages2","/sites/search/pages/default.aspx",function(searchCenterUrl,searchPageProps){
         console.log('Search Center Url:' + searchCenterUrl);
         console.log('Results Page Url:' + searchPageProps.ResultsPageAddress);
   },
   function (sender, args) {
        console.log("Error: " + args.get_message());
   });
0
votes

The search centre URL for a given web is stored in the Property bag for that web, on the RootWeb you can also set the search centre URL for the site.

In 2013 the keys have changed from 2010, they are now SRCH_ENH_FTR_URL_WEB and SRCH_ENH_FTR_URL_SITE respectivly. The code to set them is something like this:

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/search/pages");
web.update();     
ctx.load(web);     
ctx.executeQueryAsync(function () {
    alert("Search Settings Modified");
 },
 function() {
  alert("failed");
});