Wednesday, June 3, 2015

CAML Query filter between two dates

CAML Query filter between two dates

Method 1:-
</And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'><Today OffsetDays='-91' /></Value></Geq></And>

Method 2: Without using OffsetDays :-
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");

                Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<Where><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtStartDate + "</Value></Geq>" + "<Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtEndDate + "</Value></Leq></And></Where>";

Method 3: With using OffsetDays :-
Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<Where><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'><Today OffsetDays='-91' /></Value></Geq>" + "<Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'><Today OffsetDays='91' /></Value></Leq></And></Where>";

Method 4:-
SPQuery camlQuery = new SPQuery();
camlQuery.Query = "<Where><And><Geq><FieldRef Name="Created" /><Value Type="DateTime" IncludeTimeValue='FALSE'>" +SPUtility.CreateISO8601DateTimeFromSystemDateTime(ddtStartDate) + "</Value></Geq>" +
"<Leq><FieldRef Name="Created" /><Value Type="DateTime" IncludeTimeValue='FALSE'>" +
SPUtility.CreateISO8601DateTimeFromSystemDateTime(ddtEndDate) + "</Value></Leq></And></Where>";
SPListItemCollection collection = list.GetItems(query);

No comments:

Post a Comment