Sharepoint : Document library consists folders, which consists more than 5000 files. so i am getting threshold error when trying with csom code. And used caml query with relative url also but no solution.

GOPALAKRISHNAN BALRAJ 1 Reputation point
2021-11-16T07:12:27.84+00:00
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,210 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,293 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Echo Du_MSFT 17,226 Reputation points
    2021-11-17T07:55:21.903+00:00

    Hi @GOPALAKRISHNAN BALRAJ ,

    Welcome to Q&A Forum!

    The way to workaround this is pagination of the view. This means that we will have a row limit of the result that the query can return that should be less or equal to 5000. Once we get the first 5000 items we can do another query for the next 5000 starting from the position where the first result(page) ends.

    Below is an example PowerShell snippet that will take all items from a list using 5000 items page size.

    #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"  
       
    #Config Parameters  
    $SiteURL="https://xxxx.sharepoint.com/sites/echodu/"  
    $DocLibName="{YYYY}"  
      
    Try {  
        #Setup Credentials to connect  
        $Cred= Get-Credential  
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
       
        #Setup the context  
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
        $Ctx.Credentials = $Credentials  
          
        $list = $ctx.Web.Lists.GetByTitle($DocLibName)  
        $ctx.Load($list)  
        $ctx.ExecuteQuery()  
    	  
        #View XML  
        $qCommand = @"  
        <View Scope="RecursiveAll">  
    		<Query>  
    		</Query>  
    		<RowLimit Paged="TRUE">10</RowLimit>  
    	</View>  
    "@  
      
        #Page Position  
    	$position = $null  
       
    	#All Items  
    	$allItems = @()  
    	  
    	Do{  
    		$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery  
    		$camlQuery.ListItemCollectionPosition = $position  
    		$camlQuery.ViewXml = $qCommand  
    		  
    		#Executing the query  
    		$currentCollection = $list.GetItems($camlQuery)  
    		$ctx.Load($currentCollection)  
    		$ctx.ExecuteQuery()  
       
    		#Getting the position of the previous page  
    		$position = $currentCollection.ListItemCollectionPosition  
      
    		#Adding current collection to the allItems collection  
    		$allItems += $currentCollection  
             
            ForEach($Item in $allItems)  
            {  
                #Do Something  
                Write-Host "Id :" $Item["ID"]  
                Write-Host "File Name :" $Item["FileLeafRef"]  
                Write-Host "File Path :" $Item["FileRef"]  
                Write-Host "-----------------------------"   
            }   
    	}  
    	  
    	#The position of the last page will be Null  
        While($position -ne $null)  
    }  
      
    Catch {  
    	write-host -f Red "Error Getting List Items:" $_.Exception.Message  
    }  
    

    150111-1.jpg

    Thanks,
    Echo Du

    ===========================================

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.