how to limit the scope of an advanced search in Microsoft Search Server

I have encountered the following problem while installing and configuring a Microsoft Search Server (actually it was a MSSO Express 2008, but I think it is applicable to other versions as well). Since I did not find an answer and I found an original one, I'm writing an article about it.

The problem
Suppose that you install your MSSE in order to index the content of all the servers in your company, and then you want to serve each department with the relevant dcoument searchs. It is straightforward to define several content sources, a search scope for every source, and a search site for each of the departments of your company, configured to search only the scopes that are relevant to that department. You may define scopes so that if a user uses the simple search has a nice dropdown list with only the scopes that are relevant for his department, and if she accesses the search site for department A search site, its "advanced search" scope picker lists only the scopes that are relevant to department A. You shoudl configure security that only users in department A can access http://yourserver/sites/searchA, and only users in department B can access http://yourserver/sites/searchB.  Good. Then try to perform an advanced search selecting NONE of the scopes. you can expect an error message saying tha tyou should select at least a scope, or you should expect a search in all the scopes for department A, but Microsoft extends the search over ALL the scopes that are configured on the sharepoint server, not to those that are configured on site A, also to those that are relevant for department B, and C, and so on!

The solution
The solution is to embed a little javascript into your advanced.aspx page. this javascript will intercept the submit action, check if there is at least one checkbox ticked and, if none is found, it ticks the first N checkboxes it finds. You should configure a variable in the code with the number of the checkboxes, i.e.: of the scopes in your scope picker

<script language=javascript>
                      // magic by RPIOLA ( 20100210
                      // since microsoft search server's advanced search,
                      // if you do have a scope selector and you do not select
                      // any of the scopes, performs a global search,
                      // this javascript, inserted into a hidden webpart 
                      // on advanced.aspx, intercepts the form submit action,
                      // verifies if there is at least one checkbox ticked and,
                      // if none is found, ticks the first one, or the first N
                      // it finds. please configure into the global declaration
                      // global_ntocheck the number of the scopes you want to use
                      // as a default
                      function my_onsubmit()
                        var myresult='here comes the magic';
                        var i;
                        var thefirstone=-1;
                        var onechecked=false;
                        for(i=0; i<theForm.elements.length; i++)
                            myresult=myresult+"; " + theForm.elements[i].name;
                            alert('you should tick at least one scope... but I didn\'t find any');
                      //      alert('you should tick at least one scope... I assumed the first one');
                            for(i=thefirstone+1; i<theForm.elements.length && ntocheck>0; i++)
                        return default_onsubmit();

Open in new window

The .js can be embedded in an invisible content web part without title. The sample code is configured for 8 scopes. you should use the source editor to adapt to your scopes. I prepared a .dwp package, but this site does not allow me to upload it :-(

Happy searches!

Roberto Piola

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.