I try to add Sharepoint List Filter Web Part to page.

I'm not sure if it is Microsoft.SharePoint.Portal.WebControls.SpListFilterWebPart. How to add it to the page and configure?


        SpListFilterWebPart listfilter = new SpListFilterWebPart();
        listfilter.ListUrl = web.ServerRelativeUrl + "/StatisticsFiles";
        listfilter.ValueFieldGuid = fields.GetFieldByInternalName("ID").Id;
        listfilter.DescriptionFieldGuid = fields.GetFieldByInternalName("Title").Id;
        listfilter.ViewGuid = web.Lists.TryGetList("StatisticsFiles").Views["All Items"].ID;

        wpman.AddWebPart(listfilter, "Zone3", 2);

wpman.AddWebPart(listfilter, "Zone3", 2); --> here is a mistake as listfilter is Portal.WepControl, how to add it to the collection

Thank you for help

  • Lena, try using GenericWebPart listfilterWebPart = wpman.CreateWebPart(listfilter); before wpman.AddWebPart – Arsalan Adam Khatri Nov 7 '12 at 9:54

Use the following approach

1.) Create a visual webpart

2.) First add the following on the .ascx page

Code for .ascx page

<SharePoint:ListViewByQuery runat="server" ID="customlistview" /> 

3.)Add the following code to query your list(assuming you have created the site and web objects)

Code for .ascx.cs page

 SPList list = web.Lists.TryGetList("list_name");
 customlistview.List = list;
 SPQuery query = new SPQuery(customlistview.List.DefaultView);
 query.Query = "<Where>....Use your filtering query here.....</Where>";
 customlistview.Query = query;

Basically if this is an OOTB webpart or a web part which you created, you have to create an instance of it in your code.

Then you have to add this instactance to a webpart collection in a webpart manager on the page.

Check this link out

  • Thank you fo answer. I have updated the question according to your answer. Check it, please. – Lena Nov 7 '12 at 7:55

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.