Aidan Garnish

Collaboration Not Competition

Using SPQuery and CAML to filter and order

To get a collection of items from a list you can use SPQuery and CAML to filter and order the selected list:

            SPSite oSite = SPContext.Current.Site;
            SPWeb oWeb = oSite.OpenWeb();
            SPList oList = oWeb.Lists["ListName"];
            SPQuery oQuery = new SPQuery();
            oQuery.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy><Where>"
                + "<FieldRef Name='Title' /><Value Type='Text'></Value></Where>";
            SPListItemCollection oItems = oList.GetItems(oQuery);

To filter on date use the following to get any item that has been modified in the last 7 days:

string dateString = Microsoft.SharePoint.Utilities.
SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today.AddDays(-7));

oQuery.Query = <Where><Geq><FieldRef Name=\"Modified\"/><Value Type=\"DateTime\">"+ dateString+"</Value></Geq></Where>

Add comment

Loading