2

I want to query all items within 7 days and I have the following code which is working perfectly:

DateTime startDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);

DateTime enddate = startDate.AddDays(7);

query.Query = "<Where>" +
                 "<And>" +
                  "<Geq>" +
                      "<FieldRef Name='Data_x0020_e_x0020_skadimit_x002' />" +
                      "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + startDateFx + "</Value>" +
                  "</Geq>" +
                  "<Leq>" +
                      "<FieldRef Name='Data_x0020_e_x0020_skadimit_x002' />" +
                      "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + endDatFx + "</Value>" +
                 "</Leq> " +
               "</And>" +                
            "</Where>";

This query shows me all items within 7 days but I want to query all items from Past 7 days i want to query in the past how can I make that?

  • 1
    greater than or equal to today -7 and less than or equal to today – Eric Alexander Jul 7 '16 at 14:54
1

You can try

DateTime enddate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
DateTime startDate = enddate.AddDays(-7);

                   query.Query = "<Where>" +
                           "<And>" +
                                   "<Eq><FieldRef Name='Kompania' /><Value Type='Text'>" + selectedKompania + "</Value></Eq>" +

                           "<And>" +
                     "<Eq><FieldRef Name='Statusi' /><Value Type='Text'>" + selectedStatusi + "</Value></Eq>" +
                      "<And>" +
                                 "<Leq>" +
                                  "<FieldRef Name='Data_x0020_e_x0020_skadimit_x002' />" +
                                     "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + startDateFx + "</Value>" +
                                 "</Leq>" +
                                 "<Geq>" +
                                     "<FieldRef Name='Data_x0020_e_x0020_skadimit_x002' />" +
                                     "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + endDatFx + "</Value>" +
                                 "</Geq> " +
                               "</And>" +
                               "</And>" +
                               "</And>" +
                           "</Where>";
  • Do I need to change query now? – Nderon Hyseni Jul 7 '16 at 14:55
  • no i dont think so – Amal Hashim Jul 7 '16 at 14:56
  • unfortunately it is not showing anything anymore the content is blank – Nderon Hyseni Jul 7 '16 at 15:00
  • put a break point and check the date values are correct.. Also in the CAML query in your question I can see you are using different date variable.. make sure you are using enddate and startdate – Amal Hashim Jul 7 '16 at 15:09

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.