Friday 17 February 2012

Sharepoint - Return Items from a List

The following example returns items from the Calendar list in the current Web site if the event occurs after a specified date. To improve performance, the example uses the GetItems(SPQuery) method to reduce the scope of the query to a limited set of items. The example uses a constructor to instantiate an SPQuery object, and then assigns to the Query property of the query object a string in Collaborative Application Markup Language (CAML) that specifies the inner XML for the query (in other words, the <Where> element). After the Query property is set, the query object is passed to the GetItems method to return and display items.





string listTitle = TextBox1.Text;
SPWeb mySite = SPContext.Current.Web;
SPList myList = mySite.Lists[listTitle];

SPQuery myQuery = new SPQuery();

myQuery.Query = "<Where><Geq><FieldRef Name = \"EventDate\"/>" +
"<Value Type = \"DateTime\">2010-06-01</Value></Geq></Where>";

SPListItemCollection myItems = myList.GetItems(myQuery);

for (int i = 0; i < myItems.Count; i++)
{
SPListItem item = myItems[i];

Label1.Text += SPEncode.HtmlEncode(item["Title"].ToString()) + " : " +
SPEncode.HtmlEncode(item["Start Time"].ToString()) + " : " +
SPEncode.HtmlEncode(item["End Time"].ToString()) + "<BR>";
}





The previous example assumes the existence of a text box that can be used to type the name of a list, and a label to display items that are returned. Indexers are used both to return the list that is typed by the user and to enumerate the item collection. To work with individual items, indexers are used to specify the names of columns from which to retrieve values. In this case, the specified field names pertain to a standard SharePoint Foundation Calendar list.

The following example returns only Title column values in cases where the Stock column value surpasses 100.






SPWeb mySite = SPContext.Current.Web;
SPList list = mySite.Lists["Books"];

SPQuery query = new SPQuery();
query.Query = "<Where><Gt><FieldRef Name='Stock'/><Value Type='Number'>100</Value></Gt></Where>";

SPListItemCollection myItems = list.GetItems(query);

foreach (SPListItem item in myItems)
{
Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
}





The example assumes the existence of a Books list that has a Stock column that contains number values.

 

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 from a standard SharePoint Foundation Tasks list (specified by ServerTemplate = "107") where the Status column equals "Completed". For a list of the SharePoint Foundation server templates, see SPListTemplateType.




SPWeb webSite = SPContext.Current.Web;
SPSiteDataQuery query = new SPSiteDataQuery();

query.Lists = "<Lists ServerTemplate=\"107\" />";
query.Query =
"<Where><Eq><FieldRef Name=\"Status\"/>" +
"<Value Type=\"Text\">Completed</Value></Eq></Where>";

System.Data.DataTable items = webSite.GetSiteData(query);

foreach (System.Data.DataRow item in items)
{
Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
}



No comments:

Post a Comment