Customizing the PlaceHolderSearchArea
最近客户需要修改search control的东西,一直找不到好的方法,今天终于有所发现,在Todd Carter的blog里有相关涉及内容,虽然不是直接修改control的内容,但感觉帮助很大,有需要的朋友也可以看看:
Customizing the PlaceHolderSearchArea
Recently I ran into a customer who wanted to customize the small search area, specifically the SmallSearchInputBox, on all the pages within his site. What should be trivial was not as strait forward as I would have thought. What I have here is a solution that I came up with however in the spirit of community if anyone has another solution they feel is better please let me know.
So the scenario is the customer has a custom scope they would like always force their users to use rather than the people, list or site searches which appear in the search combobox depending on which page they happen to navigate to. The customer found that if he just removed the control from the PlaceHolderSearchArea of his masterpage this would not remove if off all the pages. The reason for this is that a master page defines content areas (PlaceHolderSearchArea in this case), which can be overridden by child pages, as well as common areas which cannot be overridden by child pages. The common areas will appear on any page that implements the master page. For content areas a master page typically implements content that acts as a default so that a child page has an option to take the default or override with its own implementation of that area. For this issue the customer modified the default content on his master page and for some pages this has worked since they do not implement that content area, however other pages do implement that content area. The unfortunate part here is that these child pages actually implement what is already implemented at the master page so there really is no value add for them to do this (this is true at least for the Team Site Template).
So my initial thought was there would be some way to customize the control however the control is installed via a feature and although we could modify the manifest XML associated with the feature that would be a farm wide change and this is not acceptable. So modifying the already rendered controls in client side javascript was the next choice. We have two tasks, 1) we need to hide the combobox so the user cannot change the scope and 2) we need to select the custom scope. Taking the script included below and placing this in the masterpage just before the closing HTML tag we were able to hide the combobox, select the custom scope and have these changes take effect on every page that implemented this master page even if the page itself overrode the default content implementation in the master page. Note that this works and has been tested on a team site but none of the others.
<script type="text/javascript"> var cb = GetSearchComboBox(); HideSearchComboBox(cb); TrimSearchComboBox(cb); function HideSearchComboBox(cb) { cb.style.display = 'none'; } function TrimSearchComboBox(cb) { for(var i=0; i<cb.length;i++) { if(cb.options[i].text.indexOf('[[Name of Your Scope here]]')>0) { cb.options[i].selected = true; break; } } } function GetSearchComboBox() { var elements = document.getElementsByTagName('SELECT'); for(var i=0; i<elements.length;i++) if(elements[i].id.indexOf('PlaceHolderSearchArea')>0) return elements[i]; } </script> |