by Andreas

Sharepoint 2010 – Rename Contextual Search Label

One of the elements you don’t have control over in a Sharepoint 2010 environment is the label for the contextual search option in the search drop down list (“This Site: “ and the name of the current site):

image

The dynamic nature of the contextual search definition means that wherever you are in your Sharepoint solution where the search drop down list is visible you will have a “This Site: “ option.

In some cases you might want to change this text. One annoying situation is if your solution has a series of sites that share a common prefix, and this prefix is just long enough to take up all the visible real estate of the drop down box. Your users will then see “This Site: Long Site Prefix” for all sites, instead of the last part of the name which is actually unique.

This can be solved by adding some javascript to your page (non-jQuery, so no references required). Add a Content Editor web part and edit the contents by clicking the HTML drop down and selecting “Edit HTML source”:

image

Insert the following javascript:

<script type="text/javascript">
function SearchScDDLInj()
{
    var srhScDDL = document.getElementById("ctl00_PlaceHolderSearchArea_ctl01_SBScopesDDL");
    if (srhScDDL != null)
    {
        srhScDDL.options[0].text = "My custom value";
    }
}

SearchScDDLInj();

</script>

This script will search for the drop down list (identified by ‘ctl00_PlaceHolderSearchArea_ctl01_SBScopesDDL’) and replace the first value with “My custom label”.

Save the webpart and the search drop down list should now display your own value. If not, use something like the developer tools in IE and search for the correct id for the element (not name!). The easiest way is to search for whatever value currently displayed for contextual search (“This Site: Prototyping” in my case”) and then get the ID of the placeholder:

image

 

And there you go:

image