0

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

1

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");
0

Set IncludeTimeValue to TRUE.

  • Hi Jussi, tried it and didn't worked. The only time I see different result is when I change Recursive to RecursiveAll and the result will include folders. Filter and Sorting still is not working. imgur.com/9KTQ4Bp – lypabl Oct 2 '18 at 22:58
0

Please try Ascending="False" attribute

  • Ascending="False" Like <OrderBy> <FieldRef Name="Date_Of_x0020__Posted" Ascending="False" /> </OrderBy> – Prasad Pawale Oct 2 '18 at 9:49
  • Hi Prasad, tried this already but it is still not sorting the result. imgur.com/9KTQ4Bp – lypabl Oct 2 '18 at 22:56

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.