Let’s face it, the default DotNetNuke search module sucks. It’s confusing to configure, doesn’t do a proper full text search and doesn’t search any files you may have available for download on your site. There are third-party modules that replace the standard search, but why pay for a search that is still relatively ineffectual? Why not use the Mac-Daddy of search engines, Google, to provide a free custom search module for your DotNetNuke site? And did we mention — Free!
Getting Started
The search script in this post relies on the Google Search API. In order to use this search effectively, you will want to sign up for a free Google search API key. See the Google Code site to get one. You will use this key in the JavaScript for the search function.
Once you have your key, you will need to add a Text/HTML module (HTML Module in DNN 5…) on the page where you want the search. This script is designed so that search results appear immediately below the search field, so make sure you use a pane in DotNetNuke that is wide enough to display the results adequately.
The Script
Okay, without any fanfare, here’s the script (Watch for wrapping…):
<script src="http://www.google.com/jsapi? key=Enter Your Key Text Here" type="text/javascript"></script> <script language="Javascript" type="text/javascript"> // Script based on a blog post at: // http://www.eguanasolutions.com/DNN_Blog/EntryID/7.aspx <!-- google.load('search', '1.0'); function checkKeypress(e) { var kC = (window.event) ? event.keyCode : e.keyCode; var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE; if (kC == Esc) { event.keyCode=13; return; } if (kC == 13) return false; return kC; } function OnLoad() { var searchControl = new google.search.SearchControl(); var drawOptions = new google.search.DrawOptions(); drawOptions.setInput(document.getElementById("gss_input")); var options = new google.search.SearcherOptions(); options.setExpandMode (google.search.SearchControl.EXPAND_MODE_OPEN); options.setRoot(document.getElementById("gcsResults")); searchControl.setResultSetSize (google.search.Search.SMALL_RESULTSET); // Set up a site search - searches the web site pages var siteSearch = new google.search.WebSearch(); // Replace the site root with your own site URL siteSearch.setSiteRestriction("http://www.foo.bar/"); // We're only including "aspx" pages from our site. // We're specifically excluding "LinkClick" page links // (links with "fileticket") siteSearch.setQueryAddition("+filetype:aspx -fileticket"); siteSearch.setUserDefinedLabel("Web Pages: "); searchControl.addSearcher(siteSearch, options); // Configure a search of physical files available // for download var fileSearch = new google.search.WebSearch(); // Replace with your own URL, portal and folder // with the files to be searched fileSearch.setSiteRestriction ("http://www.foo.bar/portals/0/Documents"); fileSearch.setQueryAddition("-filetype:aspx"); fileSearch.setUserDefinedLabel("Files: "); searchControl.addSearcher(fileSearch, options); searchControl.draw(null, drawOptions); } google.setOnLoadCallback(OnLoad, true); --> </script> <div> // Presentation text to appear before the search // input field, change it as you wish <p>To search all of foo.bar for both web pages and documents, enter a search term below:</p> <input type="text" value="Search Now" size="80" onfocus="if (this.value==this.defaultValue) this.value='';" id="gss_input" onkeypress="return checkKeypress(event)" /> </div> <div id="branding"> </div> <div id="gcsResults"> </div>
Just change those few things listed in the comments and this is ready to run. If you decide to use this script, either as-is or modified, please leave the comment, crediting the Eguana Solutions blog post, intact. This script was only slightly changed from the original. The post you are reading now is not meant to steal the original blog post.
Disclaimer
As usual, this script is neither supported nor guaranteed to run on your setup. If it works, great. We’re happy for you. If not, too bad. Fix it yourself or don’t use it. Of course, if there’s an obvious error, or even an improvement, we’d love to have a comment on how to change this script.
This post blatantly exploits trademarked terms, please don’t interpret this to mean that any of these entities have given up their trademarks. If there are any objections to material in this post based on trademark, copyright, offensive language or the foolishness of the author, please feel free to let us know.
Hi. Glad you like the solution. I’d appreciate a mention/credit of the original source blog postings from http://googleAJAXsearch.com (or to our general DNN blog site, http://eguanasolutions.com).
Cheers!
-mamlin
Hi,
I have got my Google API but when I add the code above with the amendements indicated I get an error on the page as follows :
Line: 552
Char: 1
Error: ‘google.search.SearchControl’ is null or not an object
Code:0
URL: http://www.insidertech.co.uk/SearchResults/tabid/37/Deafult.aspx
Sounds like you have an error in the first line, make sure it is entered correctly and that you have replaced the key listed with your key:
<script src="http://www.google.com/jsapi?key=Enter Your Key Text Here” type=”text/javascript”>
The post has also been edited to indicate this better.
Pretty cool post. I just stumbled upon your blog and wanted to say
that I have really liked reading your blog posts. Anyway
I’ll be subscribing to your blog and I hope you post again soon!