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.

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 AssembliesAdd-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 -ForceFunction 
Export-VersionHistory() {  
    param    (
        [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)        
        $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()
                        $status = $Version.FieldValues["Status"]
                        #Make sure that the status is not empty. You dont need the versions where you did not update the content
                        if($status){
                            #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"
    $ListName="ListName"
    $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"
    

Source: http://www.sharepointdiary.com


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.