Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a CAML query, in which the <Where> node looks like:

<Where>
  <Leq>
    <FieldRef Name='PublishedDate'/>
    <Value Type='DateTime'>
      <Today/>
    </Value>
  </Leq>
</Where>

Edited For Clarity:

The same query using <Now /> did not return any list values at all.

<Where>
  <Leq>
    <FieldRef Name='PublishedDate'/>
    <Value IncludeTimeValue='TRUE' Type='DateTime'>
      <Now />
    </Value>
  </Leq>
</Where>

I tried this with and without the IncludeTimeValue parameter. There are items in the list that were published months ago, so if this were a time zone issue, there should still be returned items.

Why would no values be returned?

The only similar article I could find is Using the Now() function in CAML Query? but that article does not solve my time stamp problem. Any ideas?

Edit:

After running the query in U2U my results have been validated. See the images below.

The CAML Query using <Today/>: The CAML Query using Today

The results from the query using <Today/> (note the date stamps go back months): The results using Today, note the date stamps

The results of the query after changing <Today/> to <Now/>: The results using Now - none returned

There are no results returned using <Now/>... I'm trying to filter blog posts based on the PublishedDate. The time stamp needs to be included in this comparison, requiring me to use <Now/>.

share|improve this question
1  
Interestingly enough, using an ISO 8601 date time string in place of <Now/> returns the desired results, but why doesn't the built-in <Now/> function work properly? –  RJ Cuthbertson Oct 19 '12 at 12:45
    
What was your working CAML for this? I'm doing something similar and need to filter by Now but not in a calendar setting. –  PirateEric Apr 3 '13 at 19:14
    
A little late on this @PirateEric, but the successful query I used just concatenated the C# DateTime.Now value into the string. Same query as above, but with a string value for the DateTime instead of a CAML function. (ie: ..."<Value>" + DateTime.Now.ToString(ISO8601FormatString) + "</Value>"...) –  RJ Cuthbertson Sep 25 '13 at 2:24
add comment

4 Answers 4

up vote 7 down vote accepted
+50

Although it is not mentioned directly in the MSDN documentation, the <Now/> element only takes effect when used inside the DateRangesOverlap element. Please have a look at the CAML example given in both links. When used outside the tags, where you would normally use a <Today/> or a <Month/> element, SharePoint simply ignores it which makes us feel like there is a bug.

The Now, Month and Today elements are basically used by SharePoint to display calendar views. A very interesting post about this can be found here: http://pholpar.wordpress.com/2008/03/04/using-month-element-in-daterangesoverlap-can-return-items-not-in-the-specified-month/

share|improve this answer
add comment

When you say that it didn't work, does it fail to parse, or does it simply not match your desired data? <Value IncludeTimeValue='TRUE' Type='DateTime'><Today /></Value> should work, I tested this in a query in U2U and it worked fine. I would check <Geq> as well as <Leq> to make sure it's not an issue of timezone. SharePoint's storage of times can get funny with timezones.

Check here for some relevant info: CAML Returning Tomorrow's Value when filtering Today

share|improve this answer
    
Today does work as expected. Using <Now/> returned no values. The query parses fine. The list has items published months ago, so even if the date was slightly off, it should still return me earlier values. I'm just trying to filter the results by the published date on a blog. –  RJ Cuthbertson Oct 18 '12 at 21:38
    
I do appreciate the link though. +1 for that. –  RJ Cuthbertson Oct 19 '12 at 12:55
add comment

Unfortunately, according to following article 'Now' Element doesn't exist. I had the same issue yesterday.

Writing CAML Queries For Retrieving List Items from a SharePoint List

EDIT

I found the original article. Written by Karine Bosch (SharePoint Consultant at U2U). U2U built the CAML Builder.

http: //www.u2u.be/res/Article.aspx?ART=WritingCAMLQueries

EDIT 2:

Did further investigations.

1.I had an older version of CAML Builder. New version which supports SharePoint 2010 can be downloaded from here.

2.Below code worked in CAML Builder

<Query>
   <Where>
      <Lt>
         <FieldRef Name='EndDate' />
         <Value IncludeTimeValue='TRUE' Type='DateTime'>[now]</Value>
      </Lt>
   </Where>
</Query>

3.But 'now' assumes current time is UTC and since my time zone is +11 it adds another 11 hours to it.

4.Found the source code of CAML Builder in CodePlex (may be not the current version) which may be useful.

https ://bulkdelete.svn.codeplex.com/svn/DevScope.SharePoint.Utils.BulkDelete/Helpers/CAML/CamlBuilder.cs

5.CAML Builder creates query as below when adding an offset but this doesn't work. It throws "The DateTime parameter is invalid" error.

<Query>
   <Where>
      <Lt>
         <FieldRef Name='EndDate' />
         <Value Type='DateTime'>[Now-11Hour(s)]</Value>
      </Lt>
   </Where>
</Query>

Hope this is useful.

share|improve this answer
    
msdn.microsoft.com/en-us/library/ms467521.aspx msdn.microsoft.com/en-us/library/ms461651.aspx I think Microsoft is a more reliable source. –  RJ Cuthbertson Oct 25 '12 at 2:22
    
Yes. I read that page many times. I read many forum articles which mentioned that it doesn't work. Even Microsoft can make mistakes. If you find an answer, it'll help me as well. –  Nadeeja Bomiriya Oct 25 '12 at 3:41
    
I edited above answer. –  Nadeeja Bomiriya Oct 25 '12 at 7:20
2  
@rjcup3 you put far too much faith in the CAML documentation :) –  lgaud Oct 25 '12 at 11:32
add comment

Now returns current date and time, check this Link

In the example shown in above link, we can see Now element is used for comparison. I reckon this is the way to use Now.

Whereas Today seems to return server relative date, So I would rather go with Today in your scenario.

share|improve this answer
1  
Using <Today/> instead of <Now/> is not a valid solution because it does not deal with the time of day. The PublishedDate will allow a blog user to publish their post at say 3pm. I don't want to return that post until 3pm. On the dot. Today would return the post at 12:01am. –  RJ Cuthbertson Oct 19 '12 at 12:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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