SharePoint list view web part wildcard search

As everyone knows we can do partial search on data view web part by passing a parameter to it and by using the expression "contains" we can implement partial search on a field or column. But, when you come to list view web part you can't pass the parameters  Below are the steps I followed to solve this.
  • Created an web part page in a SharePoint site.
  • Added two web parts to the page. One is for content editor web part and another is for list view web part of a SharePoint library or list.
  • Now, open the page in IE browser and add a text box, button to the content query web part.
  • Written simple logic to button click event of the button and reload the page with the below formatted url. The url should be of the format given below.
  • In above url, we are using two key value pairs to solve our problem.
  • First one is FilterName: which is for in which field of the list view we need to search the value. For example the value may be Title, ID etc…
  • Second one is the FilterMultiValue: Which is for what is the value we need to search. i.e. keyword. So, here you can pass multiple values too separated by semi-colon ';'.
  • So, example url should like this
  • http://sharepoint/pages/search.aspx?FilterName=columnname&FilterMultiValue=*keyword* 
  • Now the above url will search all the records in the given list view for the keyword "prav". So, wherever it exists in the given field name it will return all records. 
Code for single Parameter:
Create a content editor webpart
Paste the below as is in the content editor web part.

<script type="text/javascript">
function RedirectUrl()
{
var tb = document.getElementById("tbSearch").value;
if(tb != null)
{
window.location.href="?FilterName=Title&FilterMultiValue=*"+tb+"*";
}
return false;
}
</script>
<input type="text" id="tbSearch" />
<input type="button" id="btnSearch" value="search" onclick="return RedirectUrl();" />

Code For multiple parameters:

?FilterField1=columnname&FilterMultiValue1=*test* & FilterField2=columnname&FilterMultiValue2=*test1;test2*"

ex:
function ReDirectUrl()
{
var city = document.getElementById("tbSearchCity").value;
var adr = document.getElementById("tbSearchAddress").value;
var url = ""
if(city != null)
{
url = "FilterField1=City&FilterValue1=*"+city+"*";
if(adr != null)
{
url = url+"&FilterField2=Address&FilterValue2=*"+adr+"*";
// Search with two field
window.location.href="http://AllItems.aspx?"+url;
}
else
{
// Search with one field
window.location.href="http://AllItems.aspx?"+url;
}
else
{
if(adr != null)
{
url = "FilterField1=Address&FilterValue1=*"+adr+"*";
// Search with one field
window.location.href="http://AllItems.aspx?"+url;
}
}
return false;
}
}
script line
City:
<input type="text" id="tbSearchCity" />
* Address:
<input type="text" id="tbSearchAddress" />
*
<input type="button" id="btnSearch" value="search" onclick="return ReDirectUrl();" />


10 comments:

  1. Thank you for posting this. Can you describe how to insert the code you have above into the web part? I'm not a developer but this could be very valuable to me, I'm just not sure exactly how to modify the web part to a place where I can enter that code. Thanks!

    ReplyDelete
    Replies
    1. Go to Site actions-->Edit Page-->From ribbon add webpart-->select content editor webpart --> select the webpart you created in top ribbon bar you will find Html --> edit html paste the above code

      Delete
  2. I implemented the exact same code for one of my SP 2010 library and it works wonder. The only issue I have is when there is no record found matching the criteria. How could we pop a message saying no result was found? Thank you.

    ReplyDelete
  3. Hi there, I have SharePoint Online 2013 and I copied and pasted the code you wrote for a single parameter into a content editor. I changed where you have "title" to the name of my column, but when I enter in any value into the box and click the button, I get this error:

    Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED))

    Any ideas?

    ReplyDelete
    Replies
    1. I haven't tried this, but would like to suggetst you verify the column name you are given is actually the internal column name. When you create a custom column such as "Firrt Name", SharePoint would actually create it with internal column name something like "First_x200_Name". You can verify the internal column name by going to the column edit page and you should see the internal name in the URL.

      Delete
    2. Thanks to both the article editor and to Anonymous! Works great for me in a shared hosting SP2013 Foundation environment. You Rock!

      Delete
  4. So I tried to implement this in SharePoint Foundation 2013.

    I created a new Webpart Page
    I created a Listview Webpart
    Then I created a Content Editor Webpart
    I pasted the above code in the embed code
    I saved and then tested and it worked ... once

    I created a new webpart page
    I created a new listview webpart with the same columns
    I created a new content editor webpart
    I pasted the same code in the embed code
    I saved and then tested

    All it gives me is this as the following error:

    Webpage error details
    Message: Object expected
    Line: 472
    Char: 1
    Code: 0
    URI: http://myfarm/sites/mysite/SitePages/PartsTest.aspx

    I've test the URI string for example:
    http://http://myfarm/sites/mysite/SitePages/PartsTest.aspx
    ?FilterName=columnname&FilterMultiValue=*keyword*

    AND it works every time

    So what is going on that its not liking the javascript onclick?

    I am totally stumped

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. ?FilterField1=columnname&FilterMultiValue1=*test* &FilterField2=columnname&FilterMultiValue2=*test1;test2*"

    The above code for multiple multivalue parameter does not work. It works for single multivalue parameter. Any suggestions?

    ReplyDelete
  7. We are using the filter on a grouped view of a list and after applying it indicates in which groups we can find the item, but all items of that group are still shown. Do you know what could be the issue here and how we could solve it?

    ReplyDelete