Retrieve Large Number of Items from SharePoint List

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

  1. using (SPSite CurrSite = new SPSite(SPContext.Current.Web.Url))  
  2. {  
  3.    using (SPWeb CurrWeb = CurrSite.OpenWeb())  
  4.    {  
  5.        SPQuery curQry = new SPQuery();  
  6.        curQry.Query = "<Where><Eq><FieldRef Name='Dept'/> <Value Type='Text'>Sharepoint</Value></Eq></Where>";  
  7.        curQry.RowLimit = 100;  
  8.        SPList curList = CurrWeb.Lists["Dept"];  
  9.        SPListItemCollection curItems = curList.GetItems(curQry);  
  10.        foreach (SPListItem curItem in curItems)  
  11.        {  
  12.            string ResultTitle = curItem["Title"].ToString();  
  13.        }  
  14.    }  
  15. }  

 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.

  1. //Get Current Web  
  2. SPWeb curWeb = SPControl. GetContextWeb(HttpContext.Current);  
  3. //Create the Query  
  4. SPQuery curQry = new SPQuery();  
  5. curQry.Query = "<Where><Eq><FieldRef Name=\'Category\'/><Value Type=\'Text\'> Sharepoint </Value></Eq></Where>" ;  
  6.   
  7. //Get Portal Map Provider  
  8. Portal SiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;  
  9. PortalWebSiteMapNode pNode = TryCast (ps.FindSiteMapNode (curWeb. ServerRelativeUrl), PortalWebSiteMapNode);  
  10. //Get the items  
  11. Pltems = ps.GetCachedListItemsByQuery (pNode, "Dept", curQry, curWeb);  
  12. //Enumerate all resulting Items  
  13. foreach(PortalListItemSiteMapNode curItem in pItems)  
  14. {  
  15.    string ResultItemTitle = curItem["Title"]. ToString();  
  16. }  
Ebook Download
View all
Learn
View all