none
Dropdown selection to use CAML query to filter list RRS feed

  • Question

  • I need to filter list data using CAML query since there is a limit on 10 filters allowed in a view.

    Like to have a dropdown that has the following below. So if user selects "Active" it will run the CAML query I have for Active.

    I can create the CAML query just do not understand how to create a dropdown to do this.
    Really need a solid working answer that is not a bunch of link to other site. Need an answer with results that you have done and can explain in detail and show work example, either with GitHub or attach a file of the working code for me to test.

    DROPDOWN OPTION

    • Active
    • Monitored
    • 30 Days due
    • 60 Days due
    • 90 Days due
    Friday, March 10, 2017 4:10 AM

All replies

  • Hi,

    Based on my understanding, you want to filter list items based on dropdown list option dynamically, here is code snippet for your reference.

    Front-end:

    <asp:DropDownList ID="ddl_Status" AutoPostBack="true" runat="server">
        <asp:ListItem Text="Selete one item" Selected="True" Value="0"></asp:ListItem>
        <asp:ListItem Text="Active" Value="Active"></asp:ListItem>
        <asp:ListItem Text="Monitored" Value="Monitored"></asp:ListItem>
        <asp:ListItem Text="30 Days due" Value="30"></asp:ListItem>
        <asp:ListItem Text="60 Days due" Value="60"></asp:ListItem>
        <asp:ListItem Text="90 Days due" Value="90"></asp:ListItem>
    </asp:DropDownList>

    Backend:

    public string filterStatus = string.Empty; public string filterQuery = string.Empty; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack){ // you could init dropdown list here dynamically, for example //ListItem item = new ListItem("text","value"); //ddl_Status.Items.Add(item); } if (IsPostBack) { if (ddl_Status.SelectedValue != "0") { this.filterStatus = ddl_Status.SelectedValue; switch (ddl_Status.SelectedValue) { case "Active": //hard code CAML for test purpose this.filterQuery = "<Where><Eq><FieldRef Name=\"Title\" />" + "<Value Type=\"Text\">Task2</Value></Eq></Where>"; break; case "Monitored": this.filterQuery = "<Where><Eq><FieldRef Name=\"Title\" />" + "<Value Type=\"Text\">Task2.1</Value></Eq></Where>"; break; default: this.filterQuery = "<Where><Neq><FieldRef Name=\"Title\" />" + "<Value Type=\"Text\">''</Value></Neq></Where>"; break; } SPQuery query = new SPQuery(); query.Query = this.filterQuery; SPListItemCollection items = SPContext.Current.Web.Lists["MyTasks"].GetItems(query); //more logic here to process the filtered items

    } } }

    Hope this could help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, March 13, 2017 5:46 AM
  • Lee,

    This is great stuff, my only question is what do I do with backend code?

    This looks like C#. Not sure what I need to do with this code. We do not use Visual Studio in are environments, they have to be client side code and using OOTB in SharePoint. This is due to the fact client's security protocols supporting a federal client.

    I understand the ASP code but not so much on the C#. Can you provide details on how to implement?


    • Edited by 2013Sharepoint Monday, March 13, 2017 1:44 PM additional information
    Monday, March 13, 2017 11:14 AM
  • Hi,

    Sorry for responding late, for client side technology, you could try to update the viewQuery property of SP.View based on Dropdown selection, here is the link for your reference.

    https://msdn.microsoft.com/en-us/library/office/jj245986(v=office.15).aspx

    Best Regards,

    Lee

    Wednesday, March 22, 2017 8:47 AM