Wednesday, March 3, 2010

Return Items from a List

(1)The following example returns all the items for a specified Events list. It assumes the existence of a text box that can be used to type the name of an Events list. SPWeb mySite = SPContext.Current.Web; SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items; for (int i=0;i"); } (2)You can also use one of the GetItems methods of the SPList class to return a subset of items from a list. The following example returns only Title column values where the Stock column value surpasses 100. SPWeb mySite = SPContext.Current.Web; SPList list = mySite.Lists["Books"]; SPQuery query = new SPQuery(); query.Query = "100"; SPListItemCollection myItems = list.GetItems(query); foreach (SPListItem item in myItems) { Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + " "); } (3)Cross-List Queries You can perform cross-list queries to query more efficiently for data across multiple Web sites. The following example uses the SPSiteDataQuery class to define a query, and then uses the GetSiteData method to return items where the Status column equals "Completed". SPWeb webSite = SPContext.Current.Web; SPSiteDataQuery query = new SPSiteDataQuery(); query.Lists = ""; query.Query = "" + "Completed"; System.Data.DataTable items = webSite.GetSiteData(query); foreach (System.Data.DataRow item in items) { Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + " "); }

No comments: