Thursday, July 12, 2012

SharePoint 2010 - Implementing Search in a List (No Code)

This article has also been published at the NothingButSharePoint.

Description: This tutorial explains how to implement search functionality in a list without any code.

I recently had a requirement of implementing a search page for a List where user pass a search phrase via Query String (e.g. http://SharePoint2010:1000/Pages/Search.aspx?q=hello) and the search page will display the results from a List, based on that search phrase. I am excited to share the technique where one don't have to write a code to develop such page and everything can be taken care of easily via SharePoint Designer.



This technique can also be used in SharePoint 2007 via SharePoint Designer 2007. However the Data View web part's options are available on Data View context menu instead of the tool bar.



Here is the Page's code with text field and button to pass the search string to the Search.aspx page.
<script type="text/javascript">
function Redirect()
{
    window.location = "http://SharePoint2010:1000/Pages/Search.aspx?q=" + document.getElementById('Text1').value;
}
</script>

<input name="Text1" type="text">
<button name="Abutton1" onclick="Redirect();">button</button>

27 comments:

  1. nice article, just what I was finding! Any idea how to create a search button and textbox to pass that query string?

    ReplyDelete
    Replies
    1. Button:
      http://www.w3schools.com/tags/tag_button.asp

      TextBox:
      http://www.w3schools.com/jsref/dom_obj_text.asp

      Delete
  2. Great tutorial, thanks a lot for sharing!

    ReplyDelete
  3. Good one. I tried and it worked. I am new to Sharepoint. What is the best way to build a front end in Sharepoint to use the parameter to this search?
    Is there a tutorial that shows how to build a sharepoint page to ask for input of parameter "q" and click a submit button to start this search?

    ReplyDelete
    Replies
    1. I can not past HTML code in comments here. Give me your email address and I will send you a sample HTML.

      Delete
  4. steven21761@yahoo.com
    Looking forward to build my own search tool instead of using Search Center.

    ReplyDelete
    Replies
    1. I have emailed you the code that will take input from the user and will redirect to the search page with "q" parameter.

      Delete
    2. Hi Tahir ... useful tutorial thank you. May i also have the code you sent to steven please. My email address is richard@greensquirrel.co.uk

      Thanks again,

      Richard

      Delete
    3. Hi Tahir. Nice tutorial. Can you please send me the code with q parameter?
      Best regards
      kjetil.halvorsen@posten.no

      Delete
    4. Hey Tahir, great work and thanks for the article. Can you please email me the code as well?
      brian.davis@primegrp.com

      Thanks! Brian

      Delete
  5. Hi

    Please could you send me the code too?

    jodshay@gmail.com

    Thanks

    ReplyDelete
  6. Could you send me the code as well? j.david.moore1@gmail.com

    ReplyDelete
    Replies
    1. Hi Tahir ... useful tutorial, many thanks. May i also have the code you sent to steven please. My email address is marc.florie@kpn.com

      Thanks again,

      Marc

      Delete
  7. Hi Tahir.

    When i add a filter, i dont have all the fields of my list. Is there any step that i have to do in that specific field?

    and.. btw.. can you send me the HTML code to? Great tutorial!!

    pauloteixeira@mail.telepac.pt
    Thanks!!

    ReplyDelete
    Replies
    1. Edit your view to see the field.
      Also the HTML snippet has been sent.

      Delete
  8. Hi Tahir,

    Excellent tutorial! I would love a copy of the HTML code as well please, sent to eportney@gmail.com

    Thank you very much!

    ReplyDelete
  9. Hello Tahir,

    Thanks for the excellent tutorial! I hate to ask, but could I get a copy of the HTML also?

    dougg42@gmail.com

    ReplyDelete
  10. Tahir,

    This is very helpful! Would you please send the code my way? rcebarb407@gmail.com

    Thank you so much!

    ReplyDelete
    Replies
    1. The HTML code has been attached at the end of the post.

      Delete
  11. I was able to pass 2 Query String values and build an "AND" condition. i.e. SearchInvoice.aspx?Field1=abc&Field2=123

    However, in some situations, my Field2 may be "Blank", meaning I do not need to include any condition here. So the URL becomes: SearchInvoice.aspx?Field1=abc&Field2=

    When this is run, it returns NO item from SharePoint. I am hoping it will run like searching for Field1 only. I tried Field2=* but it does not work.

    Any ideas to solve this problem?

    ReplyDelete
    Replies
    1. I have seen this situation before and this is where you hit the limitation of DataForm webpart.

      DataView's variable dont understand that if the value is null then display all records. Alternate approach is to have two DataForms webparts (1 with the filter 1 without) and with JavaScript check if the value is null display the DataView webpart without the filters and it will display all results.

      Hope it helps.

      Delete

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...