0

I would like to search items between two dates in a DocumentLibrary. The query find the files at root just right. However the query doesn't seem to be considering files in folders.

Here is my attempt :

var listToQuery = web.Lists["Pages"]; 
var formatedStartDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(startDate));
var formatedEndDate   = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(endDate));

 var query = new SPQuery
            {
                Query =
                    @"<Where><And><Geq><FieldRef Name='SAG_PublicationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" +
                    formatedStartDate +
                    "</Value></Geq><Leq><FieldRef Name='SAG_PublicationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" +
                    formatedEndDate + "</Value></Leq></And></Where>",
                ViewAttributes = "scope=\"RecursiveAll\"",
                Folder = listToQuery.RootFolder
            };

   var result = listToQuery.GetItems(query).Count;

Am I missing something ? Why does the "RecursiveAll" doesn't seem to do anything ?

3
  • try scope=\"Recursive\""
    – Marko Tica
    Mar 31, 2017 at 9:33
  • remove the Folder = listToQuery.RootFolder line and try Mar 31, 2017 at 9:37
  • Same (wrong) result with Recursive instead of recursiveall and without specifying the folder.
    – Ythio Csi
    Mar 31, 2017 at 9:50

1 Answer 1

0

You are specifying the folder attribute, thats why its not querying all folders. Try it as below:

var listToQuery = web.Lists["Pages"]; 
var formatedStartDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(startDate));
var formatedEndDate   = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(endDate));

 var query = new SPQuery
            {
                Query =
                    @"<Where><And><Geq><FieldRef Name='SAG_PublicationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" +
                    formatedStartDate +
                    "</Value></Geq><Leq><FieldRef Name='SAG_PublicationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" +
                    formatedEndDate + "</Value></Leq></And></Where>",
                ViewAttributes = "scope=\"RecursiveAll\""                
            };

   var result = listToQuery.GetItems(query).Count;

Or a simpler one:

var listToQuery = web.Lists["Pages"]; 
var formatedStartDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(startDate));
var formatedEndDate   = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(endDate));


SPQuery query = new SPQuery();
query.Query = 
      @"<Where><And><Geq><FieldRef Name='SAG_PublicationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" +
                formatedStartDate +
                "</Value></Geq><Leq><FieldRef Name='SAG_PublicationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>" +
                formatedEndDate + 
                "</Value></Leq></And></Where>";
query.ViewAttributes = "Scope='RecursiveAll'";

var result = listToQuery.GetItems(query).Count;
3
  • I got the same result without specifying the folder. I tried GetItems().Count to check if i could find all files, and it worked. So basically recursion happens, and i suppose for some reason the files in the folders are rejected in the test but not the files at root, i don't know why yet
    – Ythio Csi
    Mar 31, 2017 at 9:45
  • Tried an empty query, recursion happened. I suppose the problem is in the query, maybe because folders don't have a publication date ?
    – Ythio Csi
    Mar 31, 2017 at 10:06
  • It was working, it was the test that was faulty.
    – Ythio Csi
    Mar 31, 2017 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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