Monday, January 15, 2018

SharePoint Online: Export List Version History to Excel using PowerShell

Requirement: Export all versions of SharePoint Online List Items to CSV (Excel) file.

sharepoint online export list version history to excel using powershell
PowerShell to Extract and Export List Item's Version History to CSV File:
#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://crescent.sharepoint.com"
$ListName="Projects"
$CSVFile="C:\Temp\VersionHistory.csv"

#Call the function to generate version History Report
Export-VersionHistory -SiteURL $SiteURL -ListName $ListName -CSVFile $CSVFile

Important: This script works ONLY on CSOM version 16.1.6906.1200 or Later! Download the latest version of CSOM SDK from https://www.microsoft.com/en-us/download/confirmation.aspx?id=42038

This script produces a CSV file with version data such as:
  • Version Label,
  • Version ID
  • When the version was created
  • Who created the version
  • Title field value of the version (You can add any additional field too!)



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

5 comments :

  1. Hi,

    Thanks for this code a life saver. However, there is an error in your code. The $SiteURL = "..." is wrong you have a html hyperlink ref in there when in your case it should be $SiteURL = "https://crescent.sharepoint.com".

    This caught me out for a few hours lol

    ReplyDelete
    Replies
    1. Hi There, $SiteURL="https://crescent.sharepoint.com" is what I've posted. Let me know if this is not what you are getting!

      Delete
  2. Thanks Salaudeen for the detailed PS script. When I run the Powershell, I am getting ,Cannot find an overload for "Load" and the argument count: "1"'

    ReplyDelete
    Replies
    1. Make sure you have the latest client SDK installed in your machine as per the link given in the post. Also, try running this in PowerShell ISE.

      Delete
    2. Yes, I have installed the latest client SDK (32 Bit). I wanted this PS to run as a scheduled task and I am not sure how I can achieve that. My environment is O365.

      Delete

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...