This post is about advanced filtering of SharePoint list view (or XsltListViewWebPart) by setting parameters in URL, such as:
- filter list view by part of the word (contains) in URL
- filter list view by date range in URL
- and so on.
The key is to use CAML GetVar Element.
For example, View with this <Query> will show list items whose titles contains specified in URL parameter “ProjectTitle” substring (Lists/Projects/AllItems.aspx?ProjectTitle=crm):
<Query> <Where> <Contains> <FieldRef Name="Title"/> <Value Type="Text"> <GetVar Scope="Request" Name="ProjectTitle"/> </Value> </Contains> </Where> </Query>
This View has two problems:
- It is possibly to set such View <Query> via SharePoint Designer, List Definition, custom code, but not using standard SharePoint web UI.
- If URL doesn’t contain “ProjectTitle” parameter, the View will show nothing.
We can do nothing with first problem. To fix the second use this:
<Query> <Where> <Or> <Contains> <FieldRef Name="Title"/> <Value Type="Text"> <GetVar Scope="Request" Name="ProjectTitle"/> </Value> </Contains> <!-- Added to fix bug if URL doesn't contain ProjectTitle parameter --> <Gt> <FieldRef Name="ID"/> <Value Type="Counter"> <IfEqual> <Expr1> <GetVar Scope="Request" Name="ProjectTitle"/> </Expr1> <Expr2/> <Then>0</Then> <Else>2147483647</Else> </IfEqual> </Value> </Gt> </Or> </Where> </Query>
No comments:
Post a Comment