Need to get data Snapshot of historical data every day in SharePoint Online list

Krishna Praveena Chirumamilla 1 Reputation point
2022-12-20T21:17:10.69+00:00

We are generating a custom report using PowerShell and the calculation that we used in the script is proved to be wrong. Now, we need to calculate the count using historical data. How can we get the data snapshot of the SharePoint list everyday for past month?

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

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,751 Reputation points
    2022-12-22T14:27:56.193+00:00

    Hello there,

    SharePoint lists have version history that can be turned on. Look into accessing the history via API.

    Another solution would be to use a Power Platform Dataflow on a daily basis, to query any list items with a modified date = today's date and write those records to Dataverse. More details here: https://learn.microsoft.com/en-us/power-query/dataflows/create-use

    Similar discussion here https://powerusers.microsoft.com/t5/Building-Flows/Reoccuring-export-of-SharePoint-List-Monthly-snapshot/td-p/136367

    Hope this resolves your Query !!

    ------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--


  2. Jinwei Li-MSFT 4,736 Reputation points Microsoft External Staff
    2023-01-05T09:41:57.35+00:00

    Hi @Krishna Praveena Chirumamilla

    versioning is now turned on by default when you create a new library or list, and it will automatically save the last 500 versions of a document.

    Please try to use this PowerShell to get version history details.

    #Set Parameters  
    $SiteURL="https://Crescent.sharepoint.com/sites/marketing"  
    $LibraryName="Branding"  
    $ReportOutput = "C:\Temp\VersionHistoryRpt.csv"  
        
    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  
            
        #Get the web & Library  
        $Web=$Ctx.Web  
        $Ctx.Load($Web)  
        $List = $Web.Lists.GetByTitle($LibraryName)  
        $Ctx.ExecuteQuery()  
                
        #Query to Batch process Items from the document library  
        $Query =  New-Object Microsoft.SharePoint.Client.CamlQuery  
        $Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query><RowLimit>2000</RowLimit></View>"  
       
        Do {  
            $ListItems=$List.GetItems($Query)  
            $Ctx.Load($ListItems)  
            $Ctx.ExecuteQuery()  
            $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition  
       
            $VersionHistoryData = @()  
            #Iterate throgh each file - Excluding Folder Objects  
            Foreach ($Item in $ListItems | Where { $_.FileSystemObjectType -eq "File"})  
            {  
                $File = $Web.GetFileByServerRelativeUrl($Item["FileRef"])  
                $Ctx.Load($File)  
                $Ctx.Load($File.ListItemAllFields)  
                $Ctx.Load($File.Versions)  
                $Ctx.ExecuteQuery()  
                
                Write-host -f Yellow "Processing File:"$File.Name  
                If($File.Versions.Count -ge 1)  
                {  
                    #Calculate Version Size  
                    $VersionSize = $File.Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum  
                    If($Web.ServerRelativeUrl -eq "/")  
                    {  
                        $FileURL = $("{0}{1}" -f $Web.Url, $File.ServerRelativeUrl)  
                    }  
                    Else  
                    {  
                        $FileURL = $("{0}{1}" -f $Web.Url.Replace($Web.ServerRelativeUrl,''), $File.ServerRelativeUrl)  
                    }  
        
                    #Send Data to object array  
                    $VersionHistoryData += New-Object PSObject -Property @{  
                    'File Name' = $File.Name  
                    'Versions Count' = $File.Versions.count  
                    'File Size' = ($File.Length/1KB)  
                    'Version Size' = ($VersionSize/1KB)  
                    'URL' = $FileURL  
                    }  
                }  
            }  
        } While ($Query.ListItemCollectionPosition -ne $null)  
       
        #Export the data to CSV  
        $VersionHistoryData | Export-Csv $ReportOutput -NoTypeInformation  
        
        Write-host -f Green "Versioning History Report has been Generated Successfully!"  
    }  
    Catch {  
        write-host -f Red "Error Generating Version History Report!" $_.Exception.Message  
    }  
    

    Reference:
    https://support.microsoft.com/en-us/office/how-versioning-works-in-lists-and-libraries-0f6cd105-974f-44a4-aadb-43ac5bdfd247
    https://www.sharepointdiary.com/2016/12/sharepoint-online-version-history-report-using-powershell.html


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


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.