0

I am using Powershell scripting to get data from a sharepoint list which exceeds the threshold of 5000 items.

I want to create a for loop which increments by month and year.

The Code in the for loop would move the source SharePoint list data to target SharePoint list.

I cannot completely move all the data at a time due to the limitations of SharePoint online (threshold).

I also want to ensure that all the data of that particular month gets transferred to the target list. Basically, if a month has more than 5000 items, I want the for loop to run again and move the remaining data.

Again, how should I create the for loop for the same?

Current caml query code without the for loop:

$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
     <And>
      <Gt>
       <FieldRef Name='Date' />
        <Value IncludeTimeValue='TRUE' Type='DateTime'>2020-12-01T18:37:21Z</Value>
      </Gt>
      <Lt>
       <FieldRef Name='Date' />
        <Value IncludeTimeValue='TRUE' Type='DateTime'>2020-12-31T18:37:24Z</Value>
      </Lt>
     </And>
    </Query>
    <RowLimit Paged='True'>100</RowLimit>
</View>"

Ask me more questions if needed.

1 Answer 1

2

As a workaround, you could do the loop like this:

    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Define Query to get List Items in batch
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = @"
    <View Scope='RecursiveAll'>
        <Query>
            <OrderBy><FieldRef Name='Date' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit Paged="TRUE">100</RowLimit>
    </View>
"@
 
    #Get List Items in Batch
    Do
    {
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        $ListItems.count
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
        foreach($item in $listItems)
        {
            Try
            {
              #Move item
             }
            Catch [System.Exception]
            {
            # This shouldn't happen, but just in case
            Write-Host $_.Exception.Message
            }
        }
    }
    
    While($Query.ListItemCollectionPosition -ne $null)

Reference: https://www.sharepointdiary.com/2016/12/sharepoint-online-get-all-items-from-large-lists-powershell-csom.html#ixzz6if7p4ZJu

2
  • Thanks a lot! What if I want only today's list items? I tried this code: ` $Query = New-Object Microsoft.SharePoint.Client.CamlQuery $Query.ViewXml = @" <View Scope='RecursiveAll'> <Query> <Eq> <FieldRef Name='Date' Ascending='TRUE'/> <Value IncludeTimeValue='True' Type='DateTime'><Today /></Value> </Eq> </Query> <RowLimit Paged="TRUE">5</RowLimit> </View> "@` but it returns all the values again. where am i going wrong? Commented Jan 6, 2021 at 18:08
  • @ParshvaShah you should ask that as a discrete question
    – n8.
    Commented Jul 2, 2021 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.