using (ClientContext context = new ClientContext(@"https://webApplicationName/siteName")) { context.Credentials = System.Net.CredentialCache.DefaultCredentials; List documentsLibrary = context.Web.Lists.GetByTitle("Documents"); CamlQuery query = new CamlQuery(); query.ViewXml = "<View><Query><Where><Leq><FieldRef Name='Created'/><Value Type='DateTime'><Today OffsetDays='-30' /></Value></Leq></Where></Query></View>"; //CamlQuery query = CamlQuery.CreateAllItemsQuery(); ListItemCollection expiringDocs = approvedDocumentsLibrary.GetItems(query); context.Load(expiringDocs); context.ExecuteQuery(); foreach (ListItem document in expiringDocs) { Console.WriteLine(string.Format("{0}", document["Title"])); } }
You'll notice that the crux of the filtering operation is dependent on the XML contents contained in my CamlQuery object as follows:
<View><Query><Where><Leq><FieldRef Name='Created'/><Value Type='DateTime'><Today OffsetDays='-30' /></Value></Leq></Where></Query></View>
with the <Today> element being the key to this in order to take the current date and offsetting it by 30 days in the past.
CAML Queries always take a bit of trial and error in order to get them right so I hope this helps save you some time!