Tuesday, October 6, 2009

I know, most of the SharePoint developers are looking for this. 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 [There is no support for list view web part like web part parameters] and no way we can implement the partial keyword search. Today I have spent time on it to solve this problem with the help of my colleague Durga and found a wonderful way. This is giving really wonderful result and doing wild card search even on very big document libraries fast. Below are the steps I followed to solve this. [I will post detailed post on it with screen shots and nice explanation soon in this blog.]

  • Created an ASPX 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

"you aspx page url?FilterName=FieldName&FilterMultiValue=*valuefromtextbox*".

  • 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://mosssite/pages/listviewcustomwildcardsearch.aspx?FilterName=Title&FilterMultiValue=*prav
  • 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.

Then how it is working?
Generally, If you research on query strings for a list view web part, it expects some query strings to filter and sort. I am SharePoint developer and got to know the two query strings which SharePoint list view web part uses for filtering. FilterName and FilterMultiValue. So, with that my research continued and tried long time with so many operators like %, single quotes, double quotes etc… in the value of the FilterMultiValue. Finally, I tried with * character before and end of the keyword, there I succeeded. Example: *prav* to FilterMultiValue.

  • That’s it…. Working simply great.
  • With that, You are done implementing the wild card search in a column in list view web part.

Happy by doing SharePoint customization. I know, this will solve so many problems to SharePoint dev’s.  Isn’t it? Please let me know if you have any questions, issues and write your valuable feedback and comments.

Will post more descriptive explanation of this post with screenshots soon in this blog. Follow me!!!

44 comments:

  1. Using SharePoint designer you can also promote your ListView web part to a DataView web part, which allows you to choose what parameters to use - including query string parameters.

    Here's a fairly complex example from my blog - it talks about the parameters in the lower half of the post.


    Your way is quite ingenious, though :)

    ReplyDelete
  2. Thanks for the comment.
    But for my requirement, I can't use the data view web part. There are lot of differences to both web parts in rendering and implementation. For my requirement I need to use list view web part and wild card search.
    And more over I already mentioned in my blog post like "we can implement it in data view". :)

    ReplyDelete
  3. Really awesome post. Saved so much time.

    ReplyDelete
  4. Hi Praveen,
    I have taken the same steps you have provided in your post, but is not working for me.
    It is not redirecting the page when i enter some string in the text box and click on the button.
    I have even tried editing the url directly in the browser.
    In my current example i have list which has a column Analyst and i want to get all the rows which contain "Holloway" in the analyst column.
    the url i used is "http://urlofthepage.aspx?FieldName=Analyst&FieldMultiValue=*Holloway*".
    Any help.

    ReplyDelete
  5. Hi there,
    I have corrected my post as there are minor mistakes in the url. I changed the url correctly and tested. Please try once with new url given and let me know the result. Still if you face any problem then tell me the problem. I will help you out.
    And automatically, the page won't refresh when you click submit button. Below is the logic to render the controls and rise the button click event and reload the page with new values. 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();" />

    Remember, change the url query string FilterName in the javascript to actual field name. [In your case, it is FilterName=Analyst]
    Don't forget.

    ReplyDelete
    Replies
    1. Hi Praveen,

      Thanks a lot for this post. We have implemented this and it works fine, except for one thing: 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?

      Regards,
      Anna and Mary

      Delete
  6. Hi Praveen,

    Thanks for correcting the query string I owe you one, it is working absolutely fine. But I have one other problem when i filter i have say about 60 records and at a time I am displaying on 15 records so when i click on the next page the filter is removed and it is going to the next page of the whole list.

    ReplyDelete
  7. Wonderful, It's a great find by you Praveen. But, I agree with the other guy who said about paging issue. Seems it is loosing the values when you click on page index.

    Thanks
    Bob.

    ReplyDelete
  8. Hi Praveen,

    I have one another problem also.
    There are announcement's list on the same page, when i provide a filter string and redirect it the announcement's list shows a error "One or more field types are not installed properly. Go to the list settings page to delete these fields." or some times it shows no announcements inspite the announcements list has some.

    ReplyDelete
  9. HI, i have the same problem, have you find a solution ?

    ReplyDelete
  10. Hi,

    To solve the issue that I had (The paging and the one with the Announcement's web part on the same page), I have created different views for the same list and while creating the views we can provide the filters for the list and that's how I solved my problem.
    Hope it helps.

    Thanks
    Blue Diamond.

    ReplyDelete
  11. Hi Praveen

    Nice feature - thanks.
    I'm trying to get 2 or 3 search fields work on the same time.
    Since I'm no where near a script dude I got some problems.

    The function uses FilterName and FilterMultiValue - when I manually filter on one og two columns SharePoint uses FilterField1 and FilterValue1 - repeating the fields for each filter.

    I have without luck tryed to manipulate your function to use FilterField1, FilterValue1 and 2, but maybe I need the FilterMultiValue parameter...

    Do you know if it can be done what I'm trying to do? maybe I can ask for some help with the syntax?

    Here's what I have

    script line
    function ReDirectUrl()
    {
    var city = document.getElementById("tbSearchCity").value;
    var adr = document.getElementById("tbSearchAddress").value;
    var vies = "View={871052A4-958B-4A9E-9F50-98C2EEEF9707}"&;
    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();" />

    ReplyDelete
  12. Nice post dude..!!! :)

    Did you get any ideas/solutions for the paging issue. I got a hit there. :(

    ReplyDelete
  13. You can get multiple values using this technique as well.

    For example lets say you're looking for all items with a status of "Open" and have *prav* in the description. Here's the url:

    ?FilterField1=Status&FilterValue1=Open&FilterField2=Description&FilterMultiValue2=*prav*

    Just make sure:
    a) you use unique number for each filter (1, 2, 3)
    b) the FitlerFieldN matches the FilterValueN
    c) use FilterMultiValueN to do wildcard matching

    Andy has a nice blog entry but this is low impact and can be done with a CEWP. You don't even have to create a page for this, just edit your AllItems.aspx view of the list and add the CEWP there.

    Thanks for this tip it's a great feature!

    ReplyDelete
  14. Hi Bil Simser,
    Thanks for the comment. We have tried that way, but the only thing bothered me is, how to handle this while paging. The scenario is, when we want to show the data on a specific ASPX page other than allitems.aspx and using the list view web part, I want to do filter. But if results contains more than 20, the paging will appear and when page index changed all my query strings are disappears. So, how to keep them? The post went into the confusion mode with that question.

    thanks
    -Praveen.

    ReplyDelete
  15. Hi Bil,
    I used the solution mentioned by u. My url seems like:

    "/Lists/Career%20Map/AllItems.aspx?FilterField1=TechnicalExcellence&FilterValue1=25%&FilterField2=Division&FilterMultiValue2=*DDPM;Sales*"

    But its not working
    If i change my url as shown below, it seems to be working fine

    "/Lists/Career%20Map/AllItems.aspx?FilterField1=TechnicalExcellence&FilterValue1=25%&FilterField2=Division&FilterValue2=DDPM"

    The following is also working fine:
    "/Lists/Career%20Map/AllItems.aspx?FilterField1=Division&FilterMultiValue1=*DDPM;Sales*"

    But i need my querystring as per 1st format.

    Please help.Note that data view webpart wll not work in my requirement. I need to have list view only.

    ReplyDelete
  16. Hi Bil,
    I tried the format mentioned by u.My url seems like

    "/Lists/Career%20Map/AllItems.aspx?FilterField1=TechnicalExcellence&FilterValue1=25%&FilterField2=Division&FilterMultiValue2=*DDPM;Sales*"

    But its not working.If i change my url to following its working fine

    "/Lists/Career%20Map/AllItems.aspx?FilterField1=TechnicalExcellence&FilterValue1=25%&FilterField2=Division&FilterValue2=DDPM"

    Even the following is working fine
    "/Lists/Career%20Map/AllItems.aspx?FilterField1=Division&FilterMultiValue1=*DDPM;Sales*"

    But i need my querystring in 1st format.

    Please help. Note that I cannot use dataview webpart and have 2 use listview only

    ReplyDelete
  17. Hi there,
    You need to use the FilterName and FilterMultiValue or FilterField1 and FilterValue1. FilterMultiValue works with the FilterName. So, in your querystring I am not seeing any thing related to FilterName. So, try to change the querystring and let me know.
    I am believing if you place both formats that may not work. But, give it a try like shown below.
    "/Lists/Career%20Map/AllItems.aspx?FilterField1=TechnicalExcellence&FilterMultiValue1=25%&FilterField2=Division&FilterMultiValue2=*DDPM;Sales*"

    ReplyDelete
  18. Very nice solution, but...

    Let's say you need to have two list view webparts on the same page. They both have a column with internal name 'Title'.

    How can I filter the first list view web part, but not the second one?

    Explanation: I named the first list 'Jobs'. I changed the name column 'Title' to 'Job' (internal name stays the same of course).

    The second list is named 'Employees'. Column 'Title' changed to 'Full Name'.

    I would like to filter just the values from the 'Job' column, but not the ones from 'Full Name'.

    Do you know a solution to this?
    Many thanks in advance!

    ReplyDelete
  19. What version of SharePoint are you all using? I am trying this in 2007 and the moment I sufix the paremeters with a number it stops working. The query string below fails:

    ?FilterName1=Title&FilterMultiValue1=*deliveries*

    The moment I take the "1" sufixes, it works.
    Any clues?

    Thanks!

    ReplyDelete
  20. hi,
    praveen ur solution is very use full to me but i am unable to get the filter data for discussion board (LinkDiscussionTitle-->Subject) and Document library (LinkFilename-->Name).for these columns i am unable to filtering please give me suggestion.

    ReplyDelete
  21. Awesome article! Thanks a lot for posting it.

    Cheers
    Max

    ReplyDelete
  22. Awesome work!!!!

    I need the same functionality for a form library. Could you help me out

    ReplyDelete
  23. It works fine for me.

    When i try to filter using promoted column it throws me the error :
    One or more fields types are not installed prperly.Go to the list settings page to delete these fields

    Please assist me

    ReplyDelete
  24. Great Post and useful info. However, like some I ran into columns not working (throwing error) etc. This is because sharepoint has both a 'display' name and a real 'name'when it generate a list. To search you need to use the real name. Here is some JavaScript I used for a search feature on our little farm.

    --------------
    You can pick it up as a txt as it is too big to post here.

    http://www.listedthings.com/SPsearchWild1.txt
    --------------

    Enjoy!

    Bill

    ReplyDelete
  25. Hi Praveen, is there a way to use the wild card search to retain the filter values if we select one of the values obtained by search.

    ReplyDelete
  26. Hey Praveen,

    When searching the web on this topic, I have found a third party web part that can do wildcard search on a SharePoint List view.

    The URL is http://demo.metadatasys.com

    Thanks
    Srini

    ReplyDelete
  27. Hi there.
    Great job!

    What I'm missing is the way to show documents wich are stored in folders.

    I'm wondering if this could be achieved:
    1. ASK the user for the folder name and insert it in the URL
    2. Create a new view that shows all files from all folders and use it in the URL

    In both options i'm lacking the way to modify
    window.location.href="?FilterName=DocNo&FilterMultiValue=*"+tb+"*";

    Where here can I put as a parameter the folder name or "NoFolders.aspx"?

    ReplyDelete
  28. Awesome! Worked like a charm and so easy.

    ReplyDelete
  29. Praveen,

    Did you find a solution to the pagination problem?

    I am new to Share Point and loved your code but the only issue is that the filter gets cleared when I move to other page.

    Hope you must have got a solution by now?

    Thanks anyways.

    ReplyDelete
  30. When using this method with grouping the filter does not apply to any collapsed groups, any idea how to overcome this?

    ReplyDelete
  31. I found this very helpful. Thanks very much for posting this.

    ReplyDelete
  32. This solution works really well, except when I try to search on multiple values. When I separate the values with a semicolon, it will only display the last value, unless the text in the first value is the last word in the text string. For example if I enter search values: "rate;preventive" only the Preventive line item will display. However, if I search on: "rate review;preventive", then I'll get both line items. Do I need to modify the code to ensure that I get all results or is this just a limitation of the search. Please help.

    ReplyDelete
  33. Thanks a lot for this solution.

    Thanks to Bill( cbandman_# ) for the enhanced JS to improve wildcard search.

    ReplyDelete
  34. I found this example:

    http://jasear.wordpress.com/2012/04/19/sharepoint-2010-basic-list-search-filter-webpart/

    It works with pagination.

    ReplyDelete
  35. The FilterMultiValue does NOT work for non-English keyword, for example, cannot provide a Chinese word between * and *.

    This will not work:
    http://server/DocLib1/Forms/AllItems.aspx?FilterName=_x59d3__x540d_&FilterMultiValue=*上*

    (Have used InternalName for FilterName)

    Any idea?

    - Linda

    ReplyDelete
  36. What can I do if I need to pull all records between 2 dates and a column name. EX:

    I have multiple columns for different steps that contains the date it starts.
    Column A - Column B
    01/01/2012 - 03/01/2012
    02/01/2012 - 03/15/2012

    I should be able to provide 2 filters like - Show me all records where Column A dates are between 01/01/2012 to 01/31/2012.

    How can I do this?

    ReplyDelete
  37. Hi Praveen,

    I am able to get this working for a regular list, but when I try to use it on a team discussion list (like a forum), it doesn't seem to work. Any ideas?

    Thanks,

    Eli

    ReplyDelete
  38. I will test it quickly and let you know! What field type you tested on forum list?

    -Praveen.

    ReplyDelete
  39. Praveen,

    This script works great. One question, when no result is found, how can we notify the user instead of just returning to the default page? Thanks.

    ReplyDelete
  40. Praveen,

    Did you have a chance to add the screen shots with more detailed steps? I am a newbie at SharePoint so was not able to follow some of the steps.

    Thank you!
    Dee

    ReplyDelete
  41. Hello,

    Firstly, great work Praveen! I can't thank you enough for this solution. However, multi-value searches seem to be problematic for me. Searching for val1 and val2 using the code above gives the search query *val1;val2* which is incorrect. It returns only search results for val2 or other quirky results. Also, I would rather have an AND keyword search rather than an OR - both val1 and val2 should be present in each search result, even if they are wildcards. I believe the search query should be *val1*+*val2* (correct me if I'm wrong) as described in this code below:

    function RedirectUrl()
    {
    var tb = document.getElementById("tbSearch").value;
    if(tb != null)
    {
    var arr = tb.split(";");
    var str = "";
    for (var i = 0; i < arr.length; i++)
    {
    str += "*" + arr[i] + "*";
    if(i == (arr.length - 1))
    {
    return str;
    } else {
    str += "+";
    }
    }
    window.location.href="?FilterName=Title&FilterMultiValue="+str;
    }
    return false;
    }

    - Stanley

    ReplyDelete
  42. Thanks Praveen, Spent lot of time on this. U gave me the simple solution.

    - Rajashekar

    ReplyDelete
  43. how to use or condition so make it a search box solution

    ReplyDelete