Click here to Skip to main content
15,880,543 members
Articles / Programming Languages / C#

SharePoint 2010 Working with Dates in CAML

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
10 Jul 2012CPOL2 min read 112K   572   6   4
Some of the challenges faced working with the Client Object Model.

Introduction

In this article we can explore some of the challenges faced working with the Client Object Model along with Date values.

Image 1

Challenge

You are working on a Task list which contains creation date of different months. You need to search and find the tasks of the current month. How do we achieve this using CAML at the Client Object Model?

Data

Following is the data for the Task list:

Image 2

Note: The test application contains a Create Data button to generate the list and data for you. Please ensure you are running on a test SharePoint Server.

Using the Application

You can download and run the attached source code.

Image 3

The buttons perform the following:

  • Create Data: Creates the Task list with some monthly data
  • Show All Data: Shows all the items in the Task list in the grid below
  • Show this Month Data: Shows only the current month data using CAML query filtering

CAML Query

Following is the CAML query for specifying the dates:

C#
query.ViewXml = @"<View>" +
"<Query>" +
    "<Where> <And>" +
    "<Geq>" +
        "<FieldRef Name='DueDate'/>" +
        "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + startDateFx + "</Value>" +
    "</Geq>" +
    "<Leq>" +
        "<FieldRef Name='DueDate'/>" +
        "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + endDatFx + "</Value>" +
    "</Leq>" +
    "</And> </Where>" +
"</Query>" +
"</View>";

Filters

We are using Geq (Greater than or Equal) and Leq (Less than or Equal) filters in the query.

Date Format

Please note that the dates are formatted as below:

C#
string startDateFx = startDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
string endDatFx = endDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
Please note that in the Server Object Model, we can use the SPUtility.CreateISO8601DateTimeFromSystemDateTime method to achieve the same.

CAML Query Builder

You can validate your CAML queries using the following tool:

U2U CAML Query Builder: http://www.u2u.net/res/Tools/CamlQueryBuilder.aspx.

Executing the Application

On executing the application and clicking the buttons, we can see the results below:

Image 4

The first task list displays all the items and the second task list displays only the items in the current month based on the Due Date column.

Summary

In this article we have explored the solution for handling date in the Client Object Model.  I hope the code could help you in a scenario involving Client Object Model. The attachment contains the code we have discussed.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions

 
GeneralMy vote of 3 Pin
Roman Rylov22-Sep-14 22:13
professionalRoman Rylov22-Sep-14 22:13 
GeneralDateTime format Pin
w.a.g.i18-Dec-13 3:19
w.a.g.i18-Dec-13 3:19 
GeneralQuestions about query Pin
buli_pl9-Jul-12 23:37
buli_pl9-Jul-12 23:37 
GeneralRe: Questions about query Pin
Jean Paul V.A10-Jul-12 3:35
Jean Paul V.A10-Jul-12 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.