2

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

List list = clientContext.Web.Lists.GetByTitle(listName);
clientContext.Load(list);
clientContext.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View Scope='Recursive'><Query><Where><Eq><FieldRef Name = 'Modified'/><Value Type = 'DateTime'> <Today OffsetDays='-1'/></Value ></Eq></Where></Query></View>";
ListItemCollection listCol = list.GetItems(camlQuery);
clientContext.Load(listCol);
clientContext.ExecuteQuery();
int cnt = listCol.Count;
foreach (var item in listCol)
{
          TestingFiles(clientContext, item.FieldValues["FileDirRef"].ToString());
}

Please note that the value of OffsetDays is -1.

But with the above code I am getting all the files instead only yesterdays files. Any idea what went wrong in my code. Thanks in advance.

1

I tried the Caml query commands. It works great. I can get all yesterday files (Modified = yesterday).

What is the "TestingFiles" function used for in your code?

Output all filenames from item collection to see if you get correct item results.

List list = clientContext.Web.Lists.GetByTitle(listName); 
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
    @"<View Scope='Recursive'>
    <Query>
        <Where>
        <Eq><FieldRef Name ='Modified'/><Value Type ='DateTime'><Today OffsetDays='-1'/></Value></Eq>
        </Where>
    </Query>
    </View>";
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (ListItem oListItem in listItems)
{
    Console.WriteLine(oListItem["FileRef"].ToString());
    Console.WriteLine(oListItem.FieldValues["FileDirRef"].ToString());
}

Update: entire code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Security;

namespace TestGetFiles
{
    class Program
    {


        private class Configuration
        {
            public static string ServiceSiteUrl = "https://tenant.sharepoint.com/<site>";
            public static string ServiceUserName = "admin@tenant.onmicrosoft.com";
            public static string ServicePassword = "<password>";
        }

        static ClientContext GetonlineContext()
        {
            var securePassword = new SecureString();
            foreach (char c in Configuration.ServicePassword)
            {
                securePassword.AppendChar(c);
            }
            var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);
            var context = new ClientContext(Configuration.ServiceSiteUrl);
            context.Credentials = onlineCredentials;
            return context;
        }


        static void Main(string[] args)  
        {  
            var clientContext=GetonlineContext();  
            List list = clientContext.Web.Lists.GetByTitle("<library>"); 
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml =
                @"<View Scope='Recursive'>
                <Query>
                    <Where>
                    <Eq><FieldRef Name ='DateTest'/><Value Type ='DateTime'><Today OffsetDays='-1'/></Value></Eq>
                    </Where>
                </Query>
                </View>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            foreach (ListItem oListItem in listItems)
            {
                Console.WriteLine(oListItem["FileRef"].ToString());
                Console.WriteLine(oListItem.FieldValues["FileDirRef"].ToString());
            }

            Console.ReadLine();      

        }
    }
}
  • Thanks for the response. I used the same code as yours but still the query is fetching all the files. – girish Jul 24 '18 at 6:04
  • Could you please provide the entire code you are running? – JoannaW_MSFT Jul 24 '18 at 7:41
  • I updated my reply and added my entire code. Please test my code. – JoannaW_MSFT Jul 24 '18 at 9:34
  • Please ensure you are using the latest SharePoint Online client SDK. – JoannaW_MSFT Jul 24 '18 at 9:54
  • I did the same as your code with slight changes in CQML query. Replaced DateTest with Modified but getting all the files instead yesterdays file. – girish Jul 25 '18 at 9:24
0

Please find my code as below.

private static void Testingfunction(ClientContext clientContext, string listName)
        {
            try
            {
                List list = clientContext.Web.Lists.GetByTitle(listName);
                clientContext.Load(list);
                clientContext.ExecuteQuery();
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml =
                    @"<View Scope='Recursive'>
                        <Query>
                            <Where>
                                <Eq><FieldRef Name ='Modified'/><Value Type ='DateTime'><Today OffsetDays='-1'/></Value></Eq>
                            </Where>
                        </Query>
                      </View>";
                ListItemCollection listCol = list.GetItems(camlQuery);
                clientContext.Load(listCol);
                clientContext.ExecuteQuery();
                foreach (ListItem oListItem in listCol)
                {
                    Console.WriteLine("File Name: " + oListItem["FileRef"].ToString());
                    strLog.AppendLine("File Name: " + oListItem["FileRef"].ToString());
                    Console.WriteLine("File url: " + oListItem.FieldValues["FileDirRef"].ToString());
                    strLog.AppendLine("File url: " + oListItem.FieldValues["FileDirRef"].ToString());
                    filecount++;
                }
            }
            catch (Exception ex)
            {
                strLog.AppendLine(DateTime.Now.ToString() + " Err104 - Function name -> Testing function : " + ex.Message);
            }
        }
  • There is no caml query string in camlQuery.ViewXml = @" "; in your code. You need to write query string. – JoannaW_MSFT Jul 24 '18 at 9:32
  • I updated my reply and added my entire code. Please test my code. – JoannaW_MSFT Jul 24 '18 at 9:39
  • In addition, are you using the latest SharePoint Online client SDK? – JoannaW_MSFT Jul 24 '18 at 9:52

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.