2

What I try to do is making a simple list filter tool for SharePoint 2010 lists.

What I have so far: I made a VisualWebPart (VWP) named "ListFilter", which should update the view of the list (see code below). I add the VWP to the already existing WebPartPage of a List in SharePoint.

The problem: After the first button click (btnSearchButton_Click), the list view does not update, after the second click, it does update. I already tried (as I read about page lifecycle) to put the filterList(String sw) into Page_Load or Page_Init, but without success. Maybe the cause of this is that the list renders again on postback (but as far as I know I can't influence that?!?!)

My goal: Update the list view on the first click on the button.


My Code for VWP "ListFilter":

ListFilterUserControl.ascx:

<asp:TextBox ID="tbxSearchWord" runat="server" ToolTip="Suchwort eingeben" />
&nbsp;&nbsp;
<asp:Button ID="btnSearchButton" runat="server" Text="Suche" ToolTip="Suche starten" onclick="btnSearchButton_Click" />

ListFilterUserControl.ascx.cs (code behind):

// Class Variables:
SPWeb currentWeb = SPContext.Current.Web;
String mySearchWord;
SPList list;
SPView view;

protected void Page_Load(object sender, EventArgs e)
    {
    }

protected void btnSearchButton_Click(object sender, EventArgs e)
    {
        this.mySearchWord = (String.IsNullOrEmpty(this.tbxSearchWord.Text) == false) ? this.tbxSearchWord.Text.ToString() : "No searchword entered";
        filterList(this.mySearchWord);
    }

public void filterList(String sw)
    {
        list = currentWeb.Lists["Listname"];
        view = list.Views[list.GetUncustomizedViewByBaseViewId(1).Title.ToString()];
        String query = "<Where><Contains><FieldRef Name='fieldName'/><Value Type='Text'>" + sw + "</Value></Contains></Where>";
        view.Query = query;
        currentWeb.AllowUnsafeUpdates = true;
        view.Update();
        currentWeb.AllowUnsafeUpdates = false;
    }

Mayby someone could point me to the right direction ... I'm not that experienced in programming C# / .Net. Thank you very much in advance!



EDIT

I added Response.Redirect(Request.Url.ToString()); to the filterList(String sw)-Method which reloads the page after the button click. It works now as intended, but I think it's not really a nice solution. If someone knows something better, please let me know.

1 Answer 1

1

Mike,

You can use existing Filter Web Part available OOB for this purpose. Have a look at
Connect a Filter Web Part to a List View Web Part

However if you wish to do with Visual Web Part [as sometimes the OOB isn't enough], on btnSubmit_click redirect the page to the same page with Query String of your textbox value, something like:

if (!(String.IsNullOrEmpty(this.tbxSearchWord.Text))
   Response.Redirect(SPContext.Current.Web.Url + "[pageUrl].aspx?filter=" + this.tbxSearchWord.Text.Trim());

Now edit your page in SharePoint Designer - add a filter to the List View with a parameter from Query String, parameter name "filter" !

For preserving the value of filter text on redirect, you can do something like:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["filter"]))
            {
                this.tbxSearchWord.Text = Request.QueryString["filter"];
            }
        }
    }

I hope this helps

2
  • Hi Arsalan, thanks for your answer. I'll give that a try, but in fact I want to create a reusable list filter which can be used for all lists in our company network (the code I posted was just a simplified version to show what I mean). I'm not sure if your solution would work for that purpose. Anyway thank you very much!
    – Mike
    Commented Dec 13, 2012 at 6:26
  • I guess that is not the problem, as far as you define the Filters and Query String Parameters in your List View... It should work for all the list views.. Commented Dec 13, 2012 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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