SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the code sample below, how should I change it to take a date range in a CAML query?

    string FullQueryUrlap = "<Where><And><Geq><FieldRef Name='{0}' /><Value IncludeTimeValue='TRUE' Type='DateTime'>{1}</Value></Geq><And><Leq><FieldRef Name='{2}' /><Value IncludeTimeValue='TRUE' Type='DateTime'>{3}</Value></Leq><Eq><FieldRef Name='Nazwisko' /><Value Type='Note'>{4}</Value></Eq></And></And></Where>";
        string dateISO1 = SPUtility.CreateISO8601DateTimeFromSystemDateTime(e.Day.Date.ToLocalTime());

        int spacja = DropDownList1.SelectedItem.Text.IndexOf(" ");

        var queryPoszczegolnyUrlop = new SPQuery();
        queryPoszczegolnyUrlop.Query = String.Format(FullQueryUrlap, "DataPoc", dateISO1, "DataKon", dateISO1, DropDownList1.SelectedItem.Text.ToUpper().Substring(0, spacja));
        queryPoszczegolnyUrlop.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='DataPoc' /><FieldRef Name='DataKon' />";
        queryPoszczegolnyUrlop.ViewFieldsOnly = true;

        SPListItemCollection listaDniUrlopow = witryna1.Lists["ListaUrlopow2012"].GetItems(queryPoszczegolnyUrlop);

        DateTime dataPoc= new DateTime();
        DateTime dataKon = new DateTime();

        DateTime theDate = e.Day.Date;

        for (int interator = 0; interator < listaDniUrlopow.Count; interator++)
            SPListItem item = listaDniUrlopow[interator];
            dataPoc = Convert.ToDateTime(item["DataPoc"]);
            dataKon = Convert.ToDateTime(item["DataKon"]);

The debbuger told me that listaDniUrlopow.Count have got 0 elements. Please help. When I giving that query string FullQueryUrlap = "<Where><And><Eq><FieldRef Name='{0}' /><Value Type='DateTime'>{1}</Value></Eq><Eq><FieldRef Name='Nazwisko' /><Value Type='Text'>{2}</Value></Eq></And></Where>"; it count me one element which show "dataPoc" value.

share|improve this question
This has been asked and answered here:… – Justin Stenning Nov 15 '12 at 23:11
I write as You told, but it doesn't work. Please look my answer – Grzegorz Z Nov 16 '12 at 9:58

Gt - greater operator

Lt - less operator

Look at this link for more information.

Code sample for you, you can use Geq - greater of equal and Leq - less or equal instead, depending of your case.

    <Gt><FieldRef Name='{0}' /><Value Type='DateTime'>{1}</Value></Gt>
    <Lt><FieldRef Name='{0}' /><Value Type='DateTime'>{1}</Value></Lt>
share|improve this answer
Use CAML Query Builder to make queries. It is for 2003 and 2007 SharePoint, but also works fine with SharePoint 2010 – Anton Khritonenkov Nov 16 '12 at 12:08
I'm using this, but that is need a parameters to send start, and end days. – Grzegorz Z Nov 17 '12 at 15:21

Your Answer


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.