3

I've got a Sharepoint 2010 list that I'd like to filter with date-ranges. I'm doing something like

if END_DATE > [NOW - 1Week]
and END_DATE < [NOW + 1Week]
or PLAN_END > [NOW -1Week]
and PLAN_END < [NOW +1Week]

I will post the real code at the end of this post, but as it's just a bunch of code, it doesn't have too much of real relevance.

Now, when I use conditional formatting, I can use advanced expressions to make that happen (expression see below), but when using the list-view-webpart on a webpart-page - the additional formatting gets lost, which is intended behavior as I recall.

So I do not only have to hide the entries I dont want to see, I just do not have to select them in the first place (which makes more sense anyways). But is this possible via List-Filters? I know I can edit the code and write some XML-LINQ-Like expressions (forgive my terminology, really new to all this), but I can't find out how I would write the expression I need.

To sum it up: How can I FILTER a list with Date-Ranges?

And here the actual conditional formatting that I do (Hide all, where condition is not met)

not((ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/@Plan_x0020_Ende))) >= (ddwrt:DateTimeTick(ddwrt:Today())-(7*24*60*60*1000*10000))) 
    and (ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/@Plan_x0020_Ende))) <= (ddwrt:DateTimeTick(ddwrt:Today())+(7*24*60*60*1000*10000)))) 
and not((ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/@Ist_x0020_Ende))) >= (ddwrt:DateTimeTick(ddwrt:Today())-(7*24*60*60*1000*10000))) 
    and (ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($thisNode/@Ist_x0020_Ende))) <= (ddwrt:DateTimeTick(ddwrt:Today())+(7*24*60*60*1000*10000))))

1 Answer 1

7

You can filter list by using CAML and DaysOffset attribute of the Today tag. This can be done even from GUI.

For example, this filter will narrow the shown items of a list view to those, which have Due date within a week from the current date:

enter image description here

(List Tools => List => Modify view on the contextual ribbon)

In terms of CAML, this will produce the following query:

<Query>
    <OrderBy>
        <FieldRef Name="ID"/>
    </OrderBy>
    <Where>
        <And>
            <Gt>
                <FieldRef Name="DueDate"/>
                <Value Type="DateTime">
                    <Today OffsetDays="-7"/>
                </Value>
            </Gt>
            <Lt>
                <FieldRef Name="DueDate"/>
                <Value Type="DateTime">
                    <Today OffsetDays="7"/>
                </Value>
            </Lt>
        </And>
    </Where>
</Query>

And btw, cannot resist mentioning, that the XSLT filtering approach is very expensive in case of resources (especially if there are many items in the list), and gives up the list paging. Personally, I would never ever use it in any case...

3
  • Great, thank you! This CAML_Statement is exactly what i needed - seeing this i begin to understand how this lines up :) This helps a great deal. Thank you! Far easier to write, too :P
    – Sam
    Commented Jan 6, 2012 at 10:35
  • Writing XSL to solve specific requirements can be a great idea. You need to understand the tradeoffs, as you do with any programming. Ylou don't give up paging per se; DVWPs can also have paging. Commented Jan 6, 2012 at 18:04
  • 1
    @MarcDAnderson, I meant, when you have XSLT filtering, you will either get unpredictable number of items per page, or you will have to give up the paging. Myself I'm a great fan of XSLT and XsltListViewWebPart and I'm dealing with complex XSL transformations almost every day. I definitely admire its capabilities, but it is still not a silver bullet, and I wouldn't use it for filtering or sorting. Commented Jan 6, 2012 at 19:34

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.