List view threshold error(5000 items issue) in SharePoint On-Premise/SharePoint Online

No comments

Many times when we deal with big list in SharePoint, we get the error : “This view cannot be displayed because it exceeds the list view threshold (5000 items) enforced by the administrator”. In this article we will learn about the root cause of this error and its fix.

Introduction

The threshold concept was introduced in SharePoint 2010 considering the performance issue while loading the big list to improve the query performance. There is a wrong perception about the the SharePoint list view threshold 5000 error – many users think that SharePoint list/library just can store 5000 items but that is not true, in fact SharePoint list can store 30 million items. Then why are we getting this error? Before we analyze about this error I recommend you to read my previous articles  “How does the database column indexing work in SharePoint?” and  “How to create indexed column in SharePoint?

Why do we get 5000 threshold error?

This error actually comes from SQL not from SharePoint. As I have explained in my previous article “How does the database column indexing work in SharePoint?” there we have seen that the SharePoint stores all of its list data and list columns in single SQL table, i.e. “AllUserData” table.

Now we need to learn how does the query get processed? 

When we browse a list or library SharePoint sends a query to SQL and SQL process that query and send the result to SharePoint ,then finally we can see the result i.e. nothing but list or library items in the browser. Lets take an example of “All Items” view(assumed that no filtered were applied on it) – when we click on this view SharePoint will build a query and pass this on to SQL, then SQL process that query and send the result to SharePoint. Now if the results returned by the query is more than 5000 then the user gets the above mentioned list view threshold error.

Listviewthreshold5000Error

Why does this limitation set as 5,000 in SQL?

Due to the performance reasons, if SQL server performs the query on a single table (here it is : “AllUserData” table) which would results the output more than 5000 items, in this case the SQL server will lock the entire table while the query execution is on – this is the default behavior of SQL by the designed.

So when the the “AllUserData” table is locked – what will it happen?

So far as we have learned that SharePoint stores all list/library data in a single table i.e “AllUserData” – a single query which returns more than 5,000 items will lock all of the list and library data for that entire content database.

In this scenario – the business users who were accessing content in SharePoint from the same content site collection (content database) while the “AllUserData” table is locked , they will have to wait longer time for their results to be displayed.

By now we have learned about the root cause of threshold error and we need to focus on how to move on with this situation as the business has to run.

How to over come the 5000 items list view threshold issue ?

We can increase the list items limit from 5000 to 50000(fifty thousands) in SharePoint on-premise in fact we can disable the list view threshold limit which is not all recommended but in SharePoint online we don’t have control over it, so we need to move on with the whatever limitation is set i.e. 5000.

Here based on my experience I will list out some some tips and tricks to avoid this issue which will work both for SharePoint online and on-premise.

  • Planning – before creating the list or library, analyze the data and plan accordingly. Try to split the data in multiple list or library.
  • Create multiple views and add “AND” condition in the list filter criteria, never add “OR” condition, “OR” condition will try to get the all data.
  • Create index column – we can only have 20 indexed columns in a given list. so we need plan this thoughtfully!
  • We can not create an indexed column if the list is already hitting the list view threshold limit.
  • Maintain the Archive list – here we need to review the list items periodically and need to check whether some items can be deleted or archived to another list – here we can write some automation job which will be move the items periodically to another archive list automatically.
  • In the list filter criteria we need to filter the list items such a way that at any given point of time list view will return less than 5000 items using filter on created column column technique(please see the explanation in the below).

Create filter [Today]- “number of days” 

Here one of the technique is – create a filter on “Created” column and use the [Today]- “number of days”with less than or equal to condition, something like below:

IndexingInCreatedColumn

Notes:

The logic behind the filter on created column is – this will try to get the items which will return less than 5000 items. If the this condition also returns more than 5000 items, we need to split this up created column condition with multiple “AND” condition with the combination of some other column to ensure the filter returns less than 5000 items as an below example – though the implementation of this technique is time consuming.

IndexingInCreatedColumn2

Practically – lets see when what happens when a list exceeds the 5000 items.

I have created a list named as “Test List” where adding more than 5000 items using the below PowerShell script to see what happens when it cross the 5000 limit.

CLS

#Load SharePoint CSOM Assemblies
#Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
#Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


$fileName = "Adding_Multiple_Items_Report"
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"   
$invocation = (Get-Variable MyInvocation).Value  
$directoryPath = Split-Path $invocation.MyCommand.Path 

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))  
        {  
            New-Item -ItemType directory -Path $directoryPathForLog
            
        }   
        
$logPath = $directoryPathForLog + "\" + $logFileName  
$isLogFileCreated = $False 

#DLL location
$directoryPathForDLL=$directoryPath+"\"+"Dependency Files"
if(!(Test-Path -path $directoryPathForDLL))  
        {  
            New-Item -ItemType directory -Path $directoryPathForDLL
               
        } 

#DLL location
$clientDLL=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"
$clientDLLRuntime=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"

Add-Type -Path $clientDLL
Add-Type -Path $clientDLLRuntime
  

function Write-Log([string]$logMsg)  
{   
    if(!$isLogFileCreated){   
        Write-Host "Creating Log File..."   
        if(!(Test-Path -path $directoryPath))  
        {  
            Write-Host "Please Provide Proper Log Path" -ForegroundColor Red   
        }   
        else   
        {   
            $script:isLogFileCreated = $True   
            Write-Host "Log File ($logFileName) Created..."   
            [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)   
            Add-Content -Path $logPath -Value $logMessage   
        }   
    }   
    else   
    {   
        [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)   
        Add-Content -Path $logPath -Value $logMessage   
    }   
} 


#variables region.
$siteURL="https://globalsharepoint.sharepoint.com/sites/TestSite/"
$spUserName="Global-sharepoint@globalsharepoint.onmicrosoft.com"
$password = "Pass@Word1357"
$spListName="Test List"
#variables region end.

$securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
  
#Setup the Context

try
{
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spUserName, $securePassword)
  

#Get the list
$spList = $ctx.Web.Lists.GetByTitle($spListName)
$ctx.Load($spList)
$ctx.ExecuteQuery()

# Loop to create list items
for($i=1; $i -le 5001; $i++)
{
  $listItemCreationInformationInSPOnline = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
  $newListItemInSPOnline = $spList.AddItem($listItemCreationInformationInSPOnline)
  $newListItemInSPOnline["Title"] = "CustomItemNumberAddedThruCode_$($i)"
  $newListItemInSPOnline["CustomItemNumber"] = $i;
  $newListItemInSPOnline.Update()
  $ctx.ExecuteQuery()

  write-host "Item created: CustomItemNumberAddedThruCode_$($i)"
}


}
catch 
{
  
    $errorMessage = $_.Exception.Message +"in adding mulitple items in SP Online list using CSOM PowerShell script";
    Write-Host $errorMessage -BackgroundColor Red
    Write-Log $errorMessage 
}
         
Write-Host "##########################################################"  -ForegroundColor Green 
Write-Host "The script execution has been completed!" -ForegroundColor Green 
Write-Host "##########################################################"

 

Execute the above script like below:

List View 5000 Add Items

Now if we go the list setting page (example as “Test List”), we can see the message like below which says this list exceeds the 5000 threshold limit.

ListViewThresholdMessage

Summary

Thus, in this article we have learned the below concept with respect to SharePoint list view threshold:

  • What is threshold issue in SharePoint list.
  • Why do we get 5000 threshold error?
  • Why does this limitation set as 5,000 in SQL?
  • How to over come the 5000 items list view threshold issue?
  • Practically – saw what happens when a list exceeds the 5000 items.
  • How to fix list view threshold enforced by the administrator error in SharePoint Online and On-Premise.
  • How to fix that “this view cannot be displayed because it exceeds the list view threshold (5000 items) enforced by the administrator”

Note:

Having described the threshold limit issue and their possible work around to fix this in SharePoint – its an impending open issue over a decade from Microsoft, which really Microsoft needs to fix or come up with some inbuilt alternate technique. This issue is a one of the drawbacks of SharePoint which leads many business user dissatisfaction towards SharePoint. So Microsoft really needs to fix this issue in the long run!!!