Moving versioned content to individual listitems in SharePoint

Moving versioned content to individual listitems in SharePoint

Often, it’s a good idea to version your content. And often it’s a good idea to simply, turn on versioning and the create a multiline field and selecting it to append the text. Thus creating a mini-log of something.

But, when it comes to accessing that content across items, adding attachments on a log entry. Then you have to build an actual activitylog. But the you need to move all your old content out of the single versioned items and into a new list.

Tasklist with versioning and 3 status messages.

I found a script, that I modified a little and then I used my old pal, “Fusion” to control the data, being pointed the right way. If you only have a few items, it’s pretty straight forward. But say you have a few thousand tasks with 5-10 status messages on each. Something is bound to go wrong and I like that instead of one big script to to everything, I can use PowerShell to extract the versioned content and then Fusion to import it into several different SharePoint sites and lists and then give me a report when the job is done.

NOTE: The script has been modified to be used on multiple sites. If you have many sites with many lists, use a csv that holds all the sitenames instead of just making the job run x-number of times.

#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"

# Credentials
$un = "username"
$pw = "password"
$sp = $pw | ConvertTo-SecureString -AsPlainText -Force

Function Export-VersionHistory()
        [Parameter(Mandatory=$true)] [string] $SiteName,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $CSVPath
    Try {

      $SiteURL = $SiteURL+$SiteName
      $CSVFile = $CSVPath+$SiteName+".csv"

         #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($un, $sp)
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        #Get all items
        $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $ListItems = $List.GetItems($Query)
        #Array to hold result
        $VersionHistoryData = @()
        #Iterate throgh each item
        Foreach ($Item in $ListItems)
            write-host "Processing Item:" $ -f Yellow
            #Get all versions of the list item
            $Versions = $Item.versions
            If($Versions.count -gt 0)
                #Iterate each version
                Foreach($Version in $Versions)
                    #Get the Creator object of the version
                    $CreatedBy =  $Version.createdby
                    $status = $Version.FieldValues["Status"]
                    #Make sure that the status is not empty. You dont need the versions where you did not update the content
                        #Send Data to object array
                        $VersionHistoryData += New-Object PSObject -Property @{
                            'Item ID' = $Item.ID
                            'Title' =  $Version.FieldValues["Title"]
                            'Status' =  $Version.FieldValues["Status"]
                            'Version Label' = $Version.VersionLabel 
                            'Version ID' = ($Version.VersionId/512)
                            'Created On' = (Get-Date ($Version.Created) -Format "yyyy/M/d HH:mm:ss")
                            'Created By' = $CreatedBy.Email
        #Export the data to CSV
        $VersionHistoryData | Export-Csv $CSVFile -Append -NoTypeInformation -Encoding "UTF8"
        write-host -f Green "Version History Exported Successfully to:" $CSVFile
    Catch {
        write-host -f Red "Error Exporting version History to CSV!" $_.Exception.Message
#Set global parameter values
$SiteURL="Root Site where your subsites are listet below"
$CSVPath="Folder where the csv needs to be stored"
#Call the function to generate version History Report

Export-VersionHistory -SiteURL $SiteURL -CSVPath $CSVPath -ListName $ListName -SiteName "Site 1"
Export-VersionHistory -SiteURL $SiteURL -CSVPath $CSVPath -ListName $ListName -SiteName "Site 2"
Export-VersionHistory -SiteURL $SiteURL -CSVPath $CSVPath -ListName $ListName -SiteName "Site 3"
Export-VersionHistory -SiteURL $SiteURL -CSVPath $CSVPath -ListName $ListName -SiteName "Site 4"