17

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/>.

  • 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. – Eric Alexander 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
13
+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/

3

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

  • 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
2

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.

0

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.

  • 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

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.