Question

I am creating a Console App that is retrieving all the documents under a specific document library. I want to add a filter to only retrieve the documents between a date range and I also want to sort the Modified column.

Below is my current code.

// Connecto to SharePoint
        string sourceSiteUrl = "https://XXXXX.sharepoint.com/sites/CRM";
        string password = "XXXXX";
        string userName = "XXXXX";
        SecureString securePassword = new SecureString();
        foreach (char c in password.ToCharArray())
            securePassword.AppendChar(c);

        using (var clientContext = new ClientContext(sourceSiteUrl))
        {
            clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);

            List docList = clientContext.Web.Lists.GetByTitle("TM Direction Update");

            DateTime dtStart = DateTime.Now.AddDays(-2);
            string dtStartDate = dtStart.ToString("yyyy-MM-ddTHH:mm:ssZ");
            string dtEndDate = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");

            clientContext.Load(docList); CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = string.Format(@"
            <View Scope='Recursive'>
                <Where>
                    <And>
                        <Geq>
                            <FieldRef Name='Modified' />
                                <Value Type='DateTime' IncludeTimeValue='FALSE'>{0}</Value>
                        </Geq>
                        <Leq>
                            <FieldRef Name='Modified' />
                                <Value Type='DateTime' IncludeTimeValue='FALSE'>{1}</Value>
                        </Leq>
                    </And>
                </Where>
                <OrderBy>
                    <FieldRef Name='Modified'/>
                </OrderBy>
            </View>", "2018-09-30T15:55:52Z", "2018-10-01T15:55:52Z");

            ListItemCollection listItems = docList.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

            foreach (ListItem listItem in listItems)
                Console.WriteLine("ModifiedOn: " + listItem["Modified"].ToString() + "\t" + " Id: {0} ", listItem.Id);

            Console.ReadLine();
        }

Current output of my code: enter image description here

Was it helpful?

Solution

You missed <Query></Query> tag within <View>...</View>.

Use following query:

camlQuery.ViewXml = string.Format(@"
            <View Scope='Recursive'><Query>
                <Where>
                    <And>
                        <Geq>
                            <FieldRef Name='Modified' />
                                <Value Type='DateTime' IncludeTimeValue='FALSE'>{0}</Value>
                        </Geq>
                        <Leq>
                            <FieldRef Name='Modified' />
                                <Value Type='DateTime' IncludeTimeValue='FALSE'>{1}</Value>
                        </Leq>
                    </And>
                </Where>
                <OrderBy>
                    <FieldRef Name='Modified'/>
                </OrderBy>
            </Query></View>", "2018-09-30T15:55:52Z", "2018-10-01T15:55:52Z");

OTHER TIPS

Set IncludeTimeValue to TRUE.

Please try Ascending="False" attribute

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top