Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!!!

share|improve this question

2 Answers 2

Although there is currently no API in the JavaScript CSOM which lets you change the search center url directly, SharePoint stores the search settings in the Property Bag of the root SPWeb object in a sitecollection. You can set these property bag values and your search settings should get modified.

"SRCH_ENH_FTR_URL" – Search Center URL

"SRCH_SITE_DROPDOWN_MODE" – Search Scope Drop down option

"SRCH_TRAGET_RESULTS_PAGE” – Target Search Results page

Using JavaScript CSOM, you can set the values 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","/sites/search/pages");
web.update();
ctx.load(web);

ctx.executeQueryAsync(function () {
    alert(props.get_item("SRCH_ENH_FTR_URL"));
 },
 function() {
  alert("failed");
});
share|improve this answer
    
Hey Vardhaman...thanks bunch for your response....Actually I have implemented this in exactly the way you are describing here....but Happy to hear that I also implemented the same solution.... –  nks Mar 18 '13 at 5:59
    
Though I have one issue which is really killing me when I am setting Master Page....would be great if you could throw some light on this..... Master Page Error Issue –  nks Mar 18 '13 at 6:38

Sorry for posting this as an answer, but I can't post replies yet, and this is really a reply as it does not provide an answer to the question, but rather a clarification to the answer by Vardhaman.

I tried Vardhamans code for getting the search setting without setting it, but it didn't work for me. It returned the error: "Uncaught Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."

By changing ctx.load(web) to ctx.load(props) I got it to work. Seems the real properties aren't accessible until you load the props. I also wrapped this in a SP.SOD.executeFunc which was needed for me.

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadStuff);
function loadStuff(){
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_site().get_rootWeb();
    var props =  web.get_allProperties();

    ctx.load(props);

    ctx.executeQueryAsync(function () {
        alert(props.get_item("SRCH_ENH_FTR_URL"));
    },
    function() {
        alert("failed");
    });
}

Hope this helps someone who got the same error as me, and excuse me for the new answer.

share|improve this answer
1  
Yes you are right. If you have to get the values from the property bag, you will have to do ctx.load(props) But in my case, I did not want to get any property bag values, I only wanted to set one of the values. Hence there is no need to do ctx.load(props) in case you want to set the values. –  Vardhaman Deshpande Jul 18 '13 at 9:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.