Retrieve large number of items from SharePoint List

This SharePoint tutorial explains, how to retrieve a large number of items from the SharePoint 2013/2016 list. For better performance, we can either user SPQuery or PortalSiteMapProvider Class in SharePoint.

If you have to retrieve a large number of items and also need a better performance then you should use one of the methods below:

  • Using SPQuery
  • Using PortalSiteMapProvide

Let’s see the example for both the methods to retrieve large list items.

Retrieve SharePoint list items using SPQuery:

Now, we will see how to retrieve SharePoint list items using SPQuery using the SharePoint server object model.

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();
}
}
}

Retrieve SharePoint list items using 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 the cache and returns them from the method call.

//Get Current Web
SPWeb curWeb = SPControl. GetContextWeb(HttpContext.Current);
//Create the Query
SPQuery curQry = new SPQuery();
curQry.Query = "<Where><Eq><FieldRef Name=\'Category\'/><Value Type=\'Text\'> Sharepoint </Value></Eq></Where>" ;
//Get Portal Map Provider
Portal SiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;
PortalWebSiteMapNode pNode = TryCast (ps.FindSiteMapNode (curWeb. ServerRelativeUrl), PortalWebSiteMapNode);
//Get the items
Pltems = ps.GetCachedListItemsByQuery (pNode, "Dept", curQry, curWeb);
//Enumerate all resulting Items
foreach(PortalListItemSiteMapNode curItem in pItems)
{
string ResultItemTitle = curItem["Title"]. ToString();
}

You may like following SharePoint server object model tutorials:

This SharePoint tutorial explains, how to retrieve a large number of items from the SharePoint List. The code will work with the SharePoint server object model.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

>