I would like to know how can I filter information in SharePoint list (Library of documents) programmatically. I have tried 2 ways but they don't solve my problem:
Using the string
lsWebPart.FilterString
. It creates a filter, but then the user is able to remove that filter in the interface, so it's not a good solution.Using
SPView.Query
. It modifies the view but I don't need to modify it, just to show different filters based on some user parameters.
In my site I would like to filter the same library of document by a column called 'Nationality'. So depending on the nationality of the user connected (I retrieve that from Active Directory) I would show different data. If I can' make that via code, I would need to create a view for each country (but that is not efficient and will need constant updates for new countries).
Are there other possible ways to filter the information of a list in my code?
UPDATE:
Do you mean something like that 'Aviw'?
SPListView docview = list.Views["MyView"];
var docquery = new XElement("Where",
new XElement("Eq",
new XElement("FieldRef", new XAttribute("Name", "Nationality")),
new XElement("Value", new XAttribute("Type", "String"), "xxxx")
)).ToString(SaveOptions.DisableFormatting);
docview.Query = docquery;
docview.Update();
lsWebPart.ViewGuid = docview.ID.ToString("B").ToUpper();
SOLVED:
SPList list = web.Lists["mylist"];//Evalee Docs
ListViewByQuery listviewByQuery = new ListViewByQuery();
listviewByQuery.List = list;
SPQuery query = new SPQuery(listviewByQuery.List.DefaultView);
query.Query = "Here your CAML expression";
listviewByQuery.Query = query;