Retrieve SharePoint Online items with history

Smith, Mark 1 Reputation point
2022-06-01T19:04:27.463+00:00

I have seen some scripts out there for on-prem servers that will export list items with history to a CSV file, but I can't seem to modify them to work with SharePoint Online.

Does anyone have a script that will do that? The reason for the history is because there is a column for multi-text that appends the data and it is stored in the history. So if there is a better way, I am open to it.

Thanks

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

3 answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2022-06-02T07:38:36.853+00:00

    Hi @Smith, Mark ,
    You can use the following PowerShell Script to export list items with history to a CSV file.

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12  
    #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"  
      
    Function Export-VersionHistory()  
    {  
      param  
        (  
            [Parameter(Mandatory=$true)] [string] $SiteURL,  
            [Parameter(Mandatory=$true)] [string] $ListName,  
            [Parameter(Mandatory=$true)] [string] $CSVFile  
        )  
        Try {  
      
            #Delete the Output report file if exists  
            if (Test-Path $CSVFile) { Remove-Item $CSVFile }  
      
            #Get 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  
               
            #Get the List  
            $List = $Ctx.Web.Lists.GetByTitle($ListName)  
            $Ctx.Load($List)  
            $Ctx.ExecuteQuery()  
               
            #Get all items  
            $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()  
            $ListItems = $List.GetItems($Query)  
            $Ctx.Load($ListItems)  
            $Ctx.ExecuteQuery()  
      
            #Array to hold result  
            $VersionHistoryData = @()  
      
            #Iterate throgh each item  
            Foreach ($Item in $ListItems)  
            {  
                write-host "Processing Item:" $item.id -f Yellow  
                   
                #Get all versions of the list item  
                $Versions = $Item.versions  
                $ctx.Load($Versions)  
                $Ctx.ExecuteQuery()  
      
                If($Versions.count -gt 0)  
                {  
                    #Iterate each version  
                    Foreach($Version in $Versions)  
                    {  
                        #Get the Creator object of the version  
                        $CreatedBy =  $Version.createdby  
                        $Ctx.Load($CreatedBy)  
                        $Ctx.ExecuteQuery()  
      
                        #Send Data to object array  
                        $VersionHistoryData += New-Object PSObject -Property @{  
                        'Item ID' = $Item.ID  
                        'Title' =  $Version.FieldValues["Title"]  
                        'Version Label' = $Version.VersionLabel  
                        'Version ID' = ($Version.VersionId/512)  
                        'Created On' = (Get-Date ($Version.Created) -Format "yyyy-MM-dd/HH:mm:ss")  
                        'Created By' = $CreatedBy.Email  
                        }  
                    }  
                }  
            }  
               
            #Export the data to CSV  
            $VersionHistoryData | Export-Csv $CSVFile -Append -NoTypeInformation  
      
            write-host -f Green "Version History Exported Successfully to:" $CSVFile  
         }  
        Catch {  
            write-host -f Red "Error Exporting version History to CSV!" $_.Exception.Message  
        }  
    }  
      
    #Set parameter values  
    $SiteURL="https://tenant.sharepoint.com/sites/Amy12345"  
    $ListName="test for update"  
    $CSVFile="C:\Users\spadmin\Desktop\VersionHistory.csv"  
      
    #Call the function to generate version History Report  
    Export-VersionHistory -SiteURL $SiteURL -ListName $ListName -CSVFile $CSVFile  
    

    207783-06021.png
    207775-06022.png

    Thanks,
    Yanli Jiang

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

    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.



    Updated on 2022.06.07

    According to my research and testing, the account for the credential, the domain should be consistent with the site tenant, like this:
    https://tenant.sharepoint.com/sites/sitename
    amyj@tenant.onmicrosoft.com
    And, the account should have at least [View Only] permission for this site.

    Thanks,
    Yanli Jiang

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

    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.

    1 person found this answer helpful.

  2. Smith, Mark 1 Reputation point
    2022-06-09T16:53:54.203+00:00

    I am not sure how to get past the credentials. I am a site collection admin for this location. I am getting what looks like a windows prompt also. Normally it looks different. We are using MFA if that is affecting this.

    209918-image.png


  3. Smith, Mark 1 Reputation point
    2022-06-20T13:19:55.827+00:00

    I still can't get passed the credentials. I am a site collection admin for the site and a SharePoint admin for the Tenant. So am not sure what else I would need.


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.