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

19 comments:

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   ...

Popular posts