1

I have a Search Center set up with a number of refiners. One of the refiners for the items returned identifies whether a document is considered archived or not.

It is required that a user can search both archived and unarchived documents; however, search should only display the unarchived items by default: the search page should return with the archived refiner pre-selected.

I can't modify the query for this, as applying a refiner here filters the results returned to the Search Results Web Part, and thus the Refiner panel only shows the one filtered on: "all" is not a possible selection to take the user back to the option of seeing all items.

However, I have figured out that all a refiner does is add a hash component to the URL; rewriting the URL will apply the required refiner. To this end, I've added a hidden Script Editor Web Part with the following script (Refiner works with managed property "ArchivedMP" with the value "No" (which is turned into ǂǂ4e6f somehow))

<script>
if(window.attachEvent) {
    window.attachEvent('onload', URLrewrite);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            URLrewrite();
        };
        window.onload = newonload;
    } else {
        window.onload = URLrewrite;
    }
}

function URLrewrite() {
    window.location.hash = "#Default={\"k\":\"" + getSearch("k") + "\", \"r\":[{\"n\":\"ArchivedMP\",\"t\":[\"\\\"ǂǂ4e6f\\\"\"],\"o\":\"and\",\"k\":false,\"m\":null}]}"
}

function getSearch(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (decodeURIComponent(pair[0]) == variable) {
            return decodeURIComponent(pair[1]);
        }
    }
}    
</script>

This works (after a brief flicker of the unfiltered results).

However, if a search string is entered where there exits a match on an archived document, but not an unarchived document, the search results returns no hits, and the refiner panel disappears entirely, preventing the user from being able to search the archived documents for their query. They can hit the back button, as the JavaScript above is pretty limited in scope, and doesn't do anything as complicated as track history: this will undo the automatic rewrite.

However, that's a rather clunky solution. What I'd like is to be able to retrieve the results that the Search Results Web Part is sharing with the Refiners Web Part — to share with it as well — so I can identify whether the results are null, and perhaps automatically undo the rewrite, or open up other UI elements to the user in this situation that might not be relevant if results exist.

Of course, I'm open to any other solutions too!

Your Answer

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

Browse other questions tagged or ask your own question.