Avoid using SPFolder.Item cause performance issue on large lists

When you call an “SPFolder.Item” Property ; it  executes below code once on Its parent Folder (if Parent Folder is not empty). this is a kind of Caching & Indexing mechanizm .Then returning related item from index of parent folder.

….
m_FileRefDict = new Hashtable(
StringComparer.Create(CultureInfo.InvariantCulture, true /*ignoreCase*/));

foreach (SPListItem item in items) *** Thats where we creating the long SQL query.
{
m_FileRefDict[item[SPGlobal.FieldNames.strFileRef].ToString()] = item;
}
….

And the code has to be like that . For Large lists ; in the Sitiuation has getting worse and it brings all items and makes sense of slow query for large lists specially over threshold paths.

SharePoint Supports 30,000,000 Item per list But max item count should not be exceed 5000 in a folder including root folder of the list. DocumentSets are also considered as Folder. http://technet.microsoft.com/en-us/library/cc262787(v=office.15).aspx
http://technet.microsoft.com/en-us/library/ff603637(v=office.15).aspx

In that condition our suggestions , Don’t use “SPFolder.Item” property ! which is creating performance degradation.

Instead of that :

Use “SPQuery” if you want to get the item of a SPFolder (or DocumentSet)

 For Querying a DocumentSet item you can use following query;
SPList list = Site.RootWeb.Lists["Documents"];
// Or
// SPList = Site.RootWeb.GetList(“<List URL>”)


var query = new SPQuery();
query.RowLimit = 1;
//query.Folder = Library; //if it is present in a Folder.
query.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Title' />";//Limit the returning field count for gain performance.
query.ViewFieldsOnly = true;
 query.Query = string.Format(“<Where><And><Eq><FieldRef Name=’ContentType’ /><Value Type=’Computed’>Document Set</Value></Eq><Eq><FieldRef Name=’Title’ /><Value Type=’Text’>{0}</Value></Eq></And></Where>”, documentSetName);

var itemcollection = list.GetItems(query);

foreach(SPListItem item in itemcollection)
{
Console.WriteLine(item.ID + " " + item.Title);
}

Advertisement