-1

I am working on a sharepoint farm 2013 on-premises. and i am searching for an appraoch which can allow me to build an advance search and filtering for my document library. currently my document library have the following fields:-

Built-in fields:-

  1. Title, Created, Modified, Created by & Modified by.

Custom Fields:-

  1. Document Type. of type Drop-Down.

  2. Department. of type Lookup.

  3. Owner. of type People or Group field.

now i want to have an advance search section above my list view, which can allow users to search the above columns. Now after doing some searching and reading, i find a way to do this, based on the built-in features which allow us to filter list views by passing the search/filter parameters as follow:-

/allitems.aspx?FilterField1=Type&FilterValue1=general&FilterField2Department&FilterValue2=Finance

so i am planning to add the related HTML search fields (such as Type, Department, Ower, Title, etc.. ) inside a script editor web part + java script which will be responsible to build the url based on users input, so the final url will be as shown in the above sample. Here is a quick sample of what i am planning to build (of course for the drop-down search fields i will be populating their data based on the items inside the related lookup list or based on the related site column choices):-

<script type="text/javascript">

$(document).ready(function(){

    $('#button').click(function(e) {  
        var count=1;
        var s="";

        var inputvalue = $("#doctype").val();
        var inputvalue2 = $("#title").val();
        var inputvalue3 = $("#dept").val();
        if(inputvalue!=null && inputvalue!="")
        {
        s = s+ "FilterField"+count+"=DocumentType&FilterValue"+count+"="+inputvalue+"&";
        count++;
        }
        if(inputvalue2!=null && inputvalue2!="")
        {
        s = s+ "FilterField"+count+"=title&FilterValue"+count+"="+inputvalue2+"&";
        count++;
        }
        if(inputvalue3!=null && inputvalue3!="")
        {
        s = s+ "FilterField"+count+"=Department&FilterValue"+count+"="+inputvalue3+"&";
        count++;
        }
        window.location.replace("/teamsites/Bib%20Test/Forms/search.aspx?"+s);

    });
});
</script> 

Document Type
<select id="doctype">
  <option value="general">general</option>
  <option value="history">history</option>
  <option value="budget">budget</option>
  <option value="personal">personal</option>
</select>

Title <input type="text" id="title"> 



Department
<select id="dept">
  <option value="HR">HR</option>
  <option value="Sales">Sales</option>

</select>

       <button type="button" id="button">Click Me!</button>

now i did couple of tests and the results was promising as this appraoch allowed me to:-

  1. benefit from the list view layout, as i will be filtering existing list views. so i do not have to worry about building the search result pages.

  2. the build-in list view paging will respect the original parameters which i pass. so i do not have to worry about paging.

so as i am going to use this appraoch on multiple lists mainly of type document library. so are there any drawback or limitations on using this appraoch i am unaware of? and is there a better way to manage my requirements? or my appraoch is fine to proceed with?

  • 2
    My initial thoughts are that there may be a way, through a redirect or the back button, where the user can be thrown into a view with the filters removed from the url and they will see all the items. – mannaggia Oct 26 '17 at 17:43
  • @mannaggia yes in case the users click on the list view directly they will get all the documents shown without any filter parameters... which sound valid... as no filtering/search parameters, means to show all items... – john Gu Oct 27 '17 at 9:45
  • Not sure why you posted a bounty. You've already got your answer. The answer is, yes, you can do it. – Derek Gusoff Nov 2 '17 at 13:47
1
+50

This is a clever technique to build dynamically filtered view pages without much effort or coding. You're leveraging out-of-the-box features in SharePoint in a creative way. I've done this a few times and I'd say if it meets your requirement, then go for it.

One thing to keep in mind, as mentioned in the comments, users might land on the view page by other means then your button click, and will see the unfiltered view. But I don't think this is a big deal.

  • thanks for the reply on this. so did you try similar appraoch in previous projects? as if i google advance search for document library in SP, i can not find anyone who mentioned this appraoch.. this made me worried if i will be facing issues with it. although based on my test things should be working fine... now i had a discussion with my colleague and he mentioned that there might be a limitation in the url length for 256 characters,, but i tried passing a url with filters parameters and the url length was equal to 350 characters and the list view filtering worked well.... – john Gu Oct 27 '17 at 9:50
  • second point.. now i am facing an issue , is that after the user define the filtering parameter and he clicks on the filter button, the user will be redirected to the list view with the associated documents shown, but the filter parameters will be cleared out,,, so i will be fixing this using JavaScript which will read the url and assign the filtering parameters to the fields accordingly.. in paper this should work.... what are ur thoughts on these points? – john Gu Oct 27 '17 at 9:53
  • 1
    This is why we test things. If your solutions pass the tests then they work. If your filters are not populated properly then there's something wrong with your query string - look into that. URL length shouldn't be an issue - aren't you just recreating a user action in the query string? – Derek Gusoff Oct 27 '17 at 13:18
  • no i showed a sample of the code i am planning to use.. where i will add the code inside a script editor... now when users select the filter columns and click on "click me " button, i will redirect the user to the list view with the filter parameters passed inside the url as shownwindow.location.replace("/teamsites/Bib%20Test/Forms/search.aspx?"+s);... but after the redirect the filter fields will lose there data.. as using the window.location.replace("/teamsites/Bib%20Test/Forms/search.aspx?"+s); will cause the browser to fully reload.. did u get my point ? – john Gu Oct 27 '17 at 14:00
  • 1
    you'll have to add javascript to rebind the values of the dropdowns based on the querystrings. – Mike Nov 2 '17 at 14:47
1

This was done in my projects

By using HTML Form web part you can get this:

In list edit page, then insert HTML Form web part and edit web part properties and before adding connections to list.

In SharePoint Designer,

In right above you can see views(open that list view if you are created a new view / edited same view in list) and click design view and split the design and code (you can see below left corner).

In design view click on the div above your list view, and then on the designer ribbon go to the insert tab, click on the web part button, and choose to insert the text filter web part from the filters folder on to your page.

And then click on List view and look for the Parameters on the list view tools in Option menu.

Create a new parameter and rename it, no need of default value and click OK.

After that select Filter in Option Menu and choose the columns you want to use as search fields contains.

Now, Add connection to your text filter web part by selecting Select Filter Values To.

Now, give connection to HTML Form web part in list using search parameters which we created in SPD.

Clearly explained in check this link.

1

I dont think there is any drawback in using querystring filters and also it is no code solution.

However, the condition of using if(inputvalue!=null && inputvalue!="") may give you problems if the input value gives a value like undefined.

I would recommend you to use if(inputvalue!=null && inputvalue!="" & inputvalue!=undefined)

  • or you can use if(inputvalue){} since it will return "truthy" if it is not null and has value. – Mike Nov 2 '17 at 13:45
  • No, As undefined will result in true – SharePointMan Nov 2 '17 at 13:50
  • 1
    No it doesn't. Try for yourself. var inputvalue; if(!inputvalue){console.log(1);} – Mike Nov 2 '17 at 13:54
1

I've done this before, works great and the users know that the view is a filtered view that they can clear. This is a great way to utilize the querystring functionality of SharePoint without having to manually create your own data view web part with querystring parameters. I say go for it.

I have used it in previous projects with SharePoint Designer workflows to email a link to a filtered view that is relative to the user that received the email, such as his/her group, department, or manager.

If I were to redo this script (if you don't mind) I would rewrite it this way:

<script type="text/javascript">
var count = 1;
function addfilter(inputId, internalName){

    var inputvalue = $("#" + inputId).val();
    if(inputvalue)
    {
        var s = "FilterField"+count+"=" + internalName + "&FilterValue"+count+"="+inputvalue+"&";
        count++;
        return s;
    }   
    else 
    {
        return "";
    }
}
$(document).ready(function(){

    $('#button').click(function(e) {  
        count=1;
        var s="";
        s += addfilter("doctype", "DocumentType");
        s += addfilter("title", "title");
        s += addfilter("dept", "Department");

        window.location.replace("/teamsites/Bib%20Test/Forms/search.aspx?"+s);

    });
});

  • yes changing a column title should not cause any issue,, as on the filtering parameters i will be the fields internal names...second point so have you done this appraoch before ? – john Gu Oct 31 '17 at 15:41
  • 1
    You pretty much copied my answer, right down to the word "clever". – Derek Gusoff Nov 2 '17 at 13:45
  • 1
    okay--then I'll rewrite his script. Just putting in my two cents that I've done that type of filtering before and it works great. – Mike Nov 2 '17 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.