Retrieve Large Number of Items from SharePoint List

Posted: November 7, 2014 in SharePoint2010, SharePoint2013

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. //Get Portal Map Provider
  7. Portal SiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;
  8. PortalWebSiteMapNode pNode = TryCast (ps.FindSiteMapNode (curWeb. ServerRelativeUrl), PortalWebSiteMapNode);
  9. //Get the items
  10. Pltems = ps.GetCachedListItemsByQuery (pNode, “Dept”, curQry, curWeb);
  11. //Enumerate all resulting Items
  12. foreach(PortalListItemSiteMapNode curItem in pItems)
  13. {
  14.    string ResultItemTitle = curItem[“Title”]. ToString();
  15. }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s