2 best practices loop in SharePoint list - What is the right way to iterate or loop through List Items

2 best practices loop in SharePoint list – What is the right way to iterate or loop through List Items?

No comments

Loading

2 best practices loop in SharePoint list – What is the right way to iterate or loop through List Items? In this article, we will learn what is the right way to iterate or loop through the SharePoint list items either from SharePoint on-premise or SharePoint online – o365.

Introduction – Best practices loop in SharePoint list

Generally, when a developer writes the code quickly – this might be observed. Let’s say in SharePoint “PurchaseOrder” list, there are 500 items. And thru the SharePoint object model coding, we want to read these 500 items and display the “OrderNo” column. For this, the code can be written as below:

Sample code(untested):

string strUrl = "http://server001:1111/";
using(SPSite oSite = new SPSite(strUrl))
{
using(SPWeb oWeb = oSite.OpenWeb())
{
SPList poList= oWeb.Lists["PurchaseOrder"];
for(int i=0;i<500 && i<poList.Items.Count;i++) //This is an overhead to SQL database.
{
SPListItem oneListItem = poList.Items[i];
Console.WriteLine(oneListItem ["OrderNo"])+ "\n");
}
}
}

Analysis of the above code – Best practices looping SharePoint (loop in SharePoint list)

Even though we are iterating 500 times – internally for each iteration it sends requests to the SQL database server twice i.e. one is for poList.Items.Count and another one are for the actual item. So for 500 items internally 1000 times, the above code sends the request to the SQL server which is not recommended, and hits the performance of the code.

What is the recommended way to write the above code? best practices looping SharePoint (loop in SharePoint list)

The above code can be written below way – just instead of passing the items. count inside the loop, we need to assign all 500 items in the SPListItemCollection variable – then this variable can be passed inside the loop. This way we can reduce half of the SQL server trip for the above scenario. On top of these, when the first time – SPListItemCollection gets the 500 items from SQL, it is stored in memory and gets cached, so from the subsequent looping, items are fetched from the cached memory. Hence it speeds up the execution.

Sample code(untested):

string strUrl = "http://server001:1111/";
using(SPSite oSite = new SPSite(strUrl))
{
using(SPWeb oWeb = oSite.OpenWeb())
{
SPList poList= oWeb.Lists["PurchaseOrder"];
SPListItemCollection poItems = poList.Items; //This code only one time sends the request to SQL and caches the resulted items in the memory.
for(int i=0;i<500 && i<poItems.Count;i++)
{
SPListItem oneListItem = poItems[i];
Console.WriteLine(oneListItem["OrderNo"])+ "\n"); //The individual item is being read and displayed from the "poItems" variable not directly from SQL.
}
}
}

More optimization on the above code – in fact, the most recommended one: Looping SharePoint best practices

Let’s say we have thousands of items on the list – then if we want to fetch them, we should not fetch all items at a time like above. In this scenario – we should use SPQuery or CAML (Collaborative Markup Language) query.

Sample code(untested):

string strUrl = “http://server001:1111/&#8221;;
using(SPSite oSite = new SPSite(strUrl))
{
using(SPWeb oWeb = oSite.OpenWeb())
{
SPList poList= oWeb.Lists[“PurchaseOrder”];
SPQuery query = new SPQuery();
query.RowLimit = 300; // As an example retrieve 300 items.
query.ViewFields = “OrderNo’/>”; //We can use the view fields attributes of SPQuery,when we want to retrieve the specific column from database.
SPListItemCollection poItems = poList.GetItems(query);
for(int i=0;i<500 && i<poItems.Count;i++)
{
SPListItem oneListItem = poItems[i];
Console.WriteLine(oneListItem[“OrderNo”])+ “\n”);
}
}
}

Summary: Looping SharePoint best practices (loop in SharePoint list)

Thus, in this article, we have learned that how the recommended way we can query the list item data in SharePoint.

See Also: SharePoint Online tutorial (loop in SharePoint list)

You may also like the below SharePoint Online tutorials:

Download SharePoint Online PDF Book

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.

About Post Author

Do you have a better solution or question on this topic? Please leave a comment