1

In a SharePoint 2013 publishing site, I have a custom search page.

This page can be reach from the search box in the master page. It lands on : http://server/pages/search.aspx?k=myquery, which is expected.

In this page, I have a search zone, and a result webpart. If I type a new search from this page, I land on: http://server/pages/search.aspx?k=myquery#k=mynewquery.

Is there any way to get the current query from a javascript code?

Actually, I have a custom webpart that requires to get this query. My current webpart (inherited from SP2010) relies on the query string from C# code, but this does not work with SP 2013 due to this new pure ajax page update.

Thanks for advise.

PS: my alternative would be to parse the url (looking for the k query string first after the #, then in the url query).

  • I think latter is the only option – Varun Verma Sep 9 '14 at 10:27
  • Are you saying that the Query String for "k" is not returning "mynewquery"? – Matthew McDermott Sep 13 '14 at 13:30
  • @MatthewMcDermott: yes, the query string still contains myquery – Steve B Sep 15 '14 at 8:26
2
function getQuery(group) {
return Srch.ScriptApplicationManager.get_current().queryGroups[group].dataProvider.$2_3;
}

function getDefaultQuery() {
 return getQuery("Default");
}
2

You can get the Search Query Val by Using Search OOB Object.

Srch.ScriptApplicationManager.get_current().queryGroups['Default'].dataProvider.get_currentQueryState().k

1

Using self parsing code, I can find my query using :

// In a global utility.js file
(function () {
    window.UrlQuery = window.UrlQuery || (function () {
        var result = {
            hash: {},
            query: {}
        };

        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            result.query[pair[0]] = pair[1];
        }

        var hash = window.location.hash.substring(1);
        var hashVars = hash.split("&");
        for (var i = 0; i < hashVars.length; i++) {
            var pair = hashVars[i].split("=");
            if (pair.length > 1) {
                result.hash[pair[0]] = pair[1];
            }
        }

        return result;
    })();
})();

And when I need to get the query :

        var kQuery = window.UrlQuery.hash.k || window.UrlQuery.query.k || null;
        var rQuery = window.UrlQuery.hash.r || window.UrlQuery.query.r || null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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