Use content editor webpart to search on a specific list


I recently tinkered with content editor webpart to see what kind of stuff I can achieve with this readily available webpart. I set a simple form using content editor that allows me to quickly enter a simple search term to search on a particular list. My example is to search staff resume on a resume document library in the portal.

I know when I search using my name with scope limited to resume document library, the search url will be http://myporta/people/_layouts/OSSSearchResults.aspx?k=yaohan&cs=This List&u=http:\\myportal\staff resumes

This is very useful information, because what it means is the matter of setting up a content editor webpart that allows me to build a redirection url with query string consist of the search term entered in the textbox.

I encountered a stumbling block originally, because content editor webpart does not allow form tags, since the whole SharePoint page is just one big form, thus you cannot have inner form.

So instead of using form tag, I just use a simple div as a container (not that I really need it). What you can do is just add the content editor webpart on your page, and edit the source. Place a similar code like below to display a simple textbox and a button and the javascript function will find the input textbox and build a redirection url using the value in the textbox.

 

Code Snippet:

<script type="text/javascript">
function submitCVSearch(containerElement) {

    var staffName = document.getElementsByName(‘oaktonStaffName’)[0].value;

    var searchUrl = "http://myportal/people/_layouts/OSSSearchResults.aspx?cs=This%20List&u=http%3A%2F%2Fmyportal%2Fstaff%20resumes&&quot;;

    var searchUrlForStaffCV = searchUrl + "k=" + staffName;

    window.location.href = searchUrlForStaffCV;

}
</script>

<div id="searchForm" onkeydown="javascript:if (event.keyCode==13) submitCVSearch(‘searchForm’)">
    <input name="oaktonStaffName" type="text"/><input type="button" onclick="submitCVSearch(‘searchForm’)" value="Search" />
</div>

 

This is another great way to provide simple webpart to quickly enable users to search on a specific useful list without having the results getting cluttered from other portal search results.

Leave a comment