|
There are various ways of accessing the List Items (SPListItem) in a List (SPList). Accessing list items in the same siteSPList.Items foreachThis SPListItem Collection works directly against the underlying SharePoint List (SPList). Any updates made against List Items are updated on the server. The below code shows enumerating through all List Items in a List. using (SPWeb web = siteCollection.AllWebs["webname"]) { SPListItemCollection items = web.Lists["Document Library"].Items; foreach (SPListItem item in items) { Console.Write(item.Title); } }
Accessing List Item instancesThe below code shows accessing a List Item by its identifier (Int). using (SPWeb web = siteCollection.AllWebs["webname"]) { SPList list = web.Lists["Document Library"]; SPListItem item = list.GetItemById(id); Console.Write(item.Title); }
The below code shows accessing a List Item by it's unique identifier (Guid). using (SPWeb web = siteCollection.AllWebs["webname"]) { SPList list = web.Lists["Document Library"]; SPListItem item = list.GetItemByUniqueId(guid); Console.Write(item.Title); }
SPList.Items foreach with SPQuerySPList list = SPContext.Current.Web.Lists["Contracts"]; SPQuery query = new SPQuery(); query.Query = string.Format("<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>{0}</Value></Eq></Where>","407"); SPListItem item = list.GetItems(query)[0]; SPList.Items.GetDataTable()SPListItemCollection.GetDataTable Method (Microsoft.SharePoint) using (SPWeb web = siteCollection.AllWebs["webname"]) { SPList list = web.Lists["Document Library"]; DataTable table = list.GetItems(list.DefaultView).GetDataTable(); //TODO: enumerate DataTable } SPList.Items.GetDataTable() with SPQueryReturns a copy of the list items as an ADO.NET DataTable, any updates to the DataTable do not affect the underlying SharePoint List (SPList). //TODO: PortalSiteMapProvider.GetCachedListItemsByQueryRequires Microsoft Office SharePoint Server (not WSS) using the Publishing feature. Returns a copy of the list items, any updates to the DataTable do not affect the underlying SharePoint List (SPList). PortalSiteMapProvider psmp = PortalSiteMapProvider.CurrentNavSiteMapProviderNoEncode; SPQuery query = new SPQuery { ViewFields = "<FieldRef Name='Title' /><FieldRef Name='ID' />", Query = "", // replace with your CAML query RowLimit = 10 }; SPListItemCollection listItemNodes = _portalSiteMapProvider.GetCachedListItemsByQuery( ((PortalSiteMapNode)psmp.CurrentNode).WebNode, "List Title", query, web); SPWeb.GetListItem(string url)The SPWeb.GetListItem method SPListItem listItem = SPContext.Current.Web.GetListItem(listItemUrl); Such approach for retrieving list items is extremely useful while working with Publishing Pages with elevated privileges: SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
string listItemUrl = SPContext.Current.ListItemServerRelativeUrl;
SPListItem listItem = web.GetListItem(listItemUrl);
// do something with the list item
}
}
});
List Web ServiceSee Lists SharePoint Web Service Accessing list items in multiple sitesWaldek Mastykarz: Performance of content aggregation queries on multiple lists SPSiteDataQueryReturns a copy of the list items, any updates to the DataTable do not affect the underlying SharePoint List (SPList). Source: Chakkaradeep Chandran StringBuilder queryBuilder = new StringBuilder(); SPSiteDataQuery oQuery = new SPSiteDataQuery(); oQuery.Lists = "<Lists BaseType='1'/>"; oQuery.RowLimit = 100; oQuery.Webs = "<Webs Scope=\"Recursive\" />"; queryBuilder.Append("<Where><Eq><FieldRef Name=\"UniqueId\" />"); queryBuilder.Append("<Value Type=\"Lookup\">"); queryBuilder.Append("e5d483ac-1c4a-4699-bcd1-dd0bb0455a71"); queryBuilder.Append("</Value></Eq></Where>"); oQuery.Query = queryBuilder.ToString(); DataTable dtResult = web.GetSiteData(oQuery); More on SPSiteDataQueryCrossListQueryCacheUses the same caching functionality as the Content Query Web Part and similarly requires Microsoft Office SharePoint Server 2007 Standard minimum. Returns a copy of the list items, any updates to the DataTable do not affect the underlying SharePoint List (SPList). CrossListQueryInfo clqInfo = new CrossListQueryInfo(); clqInfo.Webs = "<Webs Scope='SiteCollection'/>"; clqInfo.Lists = "<Lists ServerTemplate='101'/>"; clqInfo.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='ID' />"; clqInfo.Query = ""; // replace with your CAML query clqInfo.RowLimit = 10; clqInfo.UseCache = true; CrossListQueryCache clqCache = new CrossListQueryCache(clqInfo); DataTable resultsTable = clqCache.GetSiteData(SPContext.Current.Site);
MSDN referencesSearchReturns a copy of the list items, any updates to the DataTable do not affect the underlying SharePoint List (SPList). //TODO: External Links
Labels |
Accessing list items using the object model

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare









Comments (4)
Jul 15, 2009
Anonymous says:
Great !! simply GreatGreat !! simply Great
Jul 15, 2009
Anonymous says:
Post is satisfy from dummy's to Experts Sudip Misra sudipmisra@hotmail.comPost is satisfy from dummy's to Experts
Sudip Misra
sudipmisra@hotmail.com
Apr 01
Lars Nielsen says:
Excellent article I just stumbled on - it's so useful to have all this basic inf...Excellent article I just stumbled on - it's so useful to have all this basic info collected together on one page.
Jul 08
andy au says:
For PortalSiteMapProvider.GetCachedListItemsByQuery, how do you get SPListItemCo...For PortalSiteMapProvider.GetCachedListItemsByQuery, how do you get SPListItemCollection as a return object? I only get SiteMapNodeCollection as a return type.