I am trying to fetch all the files which is modified or added yesterday from SharePoint document library using below code.

using (ClientContext clientCtx = GetContext())
                {
                    Web oweb = clientCtx.Web;
                    var getListQuery = from list in oweb.Lists.Include(list => list.RootFolder) where list.BaseTemplate == 101 && list.Hidden == false select list;
                    var allDocLibraries = clientCtx.LoadQuery(getListQuery);
                    clientCtx.ExecuteQuery();
                    foreach (var library in allDocLibraries)
                    {
                        DateTime yesterday = DateTime.Today.AddDays(-1);
                        string rootFolder = library.RootFolder.Name;
                        var keywordQuery = new KeywordQuery(clientCtx);
                        string librarPath = clientCtx.Url + rootFolder + "/*";
                        string yesterdayDate = yesterday.Month.ToString() + "/" + yesterday.Day.ToString() + "/" + yesterday.Year;
                        keywordQuery.QueryText = "contenttype:document AND LastModifiedTime>=" + yesterdayDate + " AND path:" + HttpUtility.UrlPathEncode(librarPath, true);
                        //keywordQuery.QueryText = "ContentClass:STS_ListItem_documentLibrary AND LastModifiedTime>=" + yesterdayDate + " AND path:" + HttpUtility.UrlPathEncode(librarPath, true);
                        //keywordQuery.QueryText = "(contentclass:STS_ListItem OR IsDocument:True) AND LastModifiedTime>=" + yesterdayDate + " AND path:" + HttpUtility.UrlPathEncode(librarPath, true);
                        //keywordQuery.TrimDuplicates = false;
                        //keywordQuery.RowLimit = 500;
                        var searchExecutor = new SearchExecutor(clientCtx);
                        var searchResult = searchExecutor.ExecuteQuery(keywordQuery);
                        clientCtx.ExecuteQuery();
                        var allRecentFiles = searchResult.Value[0].ResultRows.ToList();
                        foreach (var file in allRecentFiles)
                        {
                            strLog.AppendLine("File path - " + file["OriginalPath"].ToString());
                            filecount++;
                        }
                    }
                    strLog.AppendLine("Total nummber files updated:" + filecount);
                }

But the above query fetches only few files not all the files from doc library. Any idea what went wrong in my code. Thanks in advance.

Given that you are looping through each individual document library, in this particular case I would probably opt for using a CamlQuery to get library items, instead of using the search api.

Also, clearly defining what 'yesterday' means might help too, because this may have time zone implications. By that I mean specifying the date and time in UTC as part of the query.

  • Thanks Paul. CAML query will not work if the list has more than 5K items. If there is an issue with timezone then query will not fetch any records. But in this case query is fetching few records. – girish Aug 13 at 6:23

You could try to catch the query text and then test, I think this will help you identify the issue.

For example:

contenttype:document AND LastModifiedTime>=2018-07-01T12:00:00.0000000Z AND path:http://sp:12001/mydoc3

enter image description here

  • Thanks for the response. I am not getting any result when I search in site with query text. Code I am getting results but its not fetching all the files. Fetches only 4 files out of 8. – girish Aug 17 at 8:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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