Introduction
In this blog we explore to retrieve large number of items with a better performance we can either user SPQuery or PortalSiteMapProvider Class. Read more with examples. Retrieving large number of items from SharePoint List.
If you have to retrieve a large number of items and also need a better performance then you should use one of the method below:
- Using SPQuery
- Using PortalSiteMapProvide
Let’s see the example for both the method
SPQuery
- using (SPSite CurrSite = new SPSite(SPContext.Current.Web.Url))
- {
- using (SPWeb CurrWeb = CurrSite.OpenWeb())
- {
- SPQuery curQry = new SPQuery();
- curQry.Query = "<Where><Eq><FieldRef Name='Dept'/> <Value Type='Text'>Sharepoint</Value></Eq></Where>";
- curQry.RowLimit = 100;
- SPList curList = CurrWeb.Lists["Dept"];
- SPListItemCollection curItems = curList.GetItems(curQry);
- foreach (SPListItem curItem in curItems)
- {
- string ResultTitle = curItem["Title"].ToString();
- }
- }
- }
PortalSiteMapProvide
The Class includes a method called GetCachedListItemsByQuery that retrieves data from a list based on an SPQuery object that is provided as a parameter to the method call.
The method then looks in its cache to see if the items already exist. If they do, the method returns the cached results, and if not, it queries the list , store the results in cache and returns them from the method call.
-
- SPWeb curWeb = SPControl. GetContextWeb(HttpContext.Current);
-
- SPQuery curQry = new SPQuery();
- curQry.Query = "<Where><Eq><FieldRef Name=\'Category\'/><Value Type=\'Text\'> Sharepoint </Value></Eq></Where>" ;
-
-
- Portal SiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;
- PortalWebSiteMapNode pNode = TryCast (ps.FindSiteMapNode (curWeb. ServerRelativeUrl), PortalWebSiteMapNode);
-
- Pltems = ps.GetCachedListItemsByQuery (pNode, "Dept", curQry, curWeb);
-
- foreach(PortalListItemSiteMapNode curItem in pItems)
- {
- string ResultItemTitle = curItem["Title"]. ToString();
- }