Reputation: 121
In SharePoint 2010, I need to get items from a list based on a condition. Considering one of the fields to be 'Date' of type DateTime, the condition is:
Get Current Month Data.
How do I filter the list items based on this condition using CAML query?
By, Raji
Upvotes: 1
Views: 4442
Reputation: 6301
Use SPUtility.CreateISO8601DateTimeFromSystemDateTime to create relevant dateTime string
DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
Sting stringQuery =
String.Format(@"<And>
<Geq>
<FieldRef Name='Date' />
<Value Type='DateTime'>{0}</Value>
</Geq>
<Leq>
<FieldRef Name='Date' />
<Value Type='DateTime'>{1}</Value>
</Leq>
</And>",
SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay),
SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay .AddMonths(1)));
SPQuery query = new SPQuery(stringQuery);
Upvotes: 3