导出 SharePoint 列表的版本历史事件日志

时间:2021-07-29 10:04:42

标签: sharepoint

我正在寻找事件日志或完整版本历史记录,其中包含对 SharePoint 列表项目所做的所有更改。

版本历史记录已启用。当我用鼠标右键单击一个项目并选择版本历史时,它实际上显示了我需要的所有数据:

  • 修改时间戳
  • (Column, value) 对新修改的列和它的新值
  • 由谁修改

现在我“只”需要在表格或文件(xlsx、xml、json、csv)中组织这些数据。以及列表的所有项目。

我可以看到数据在那里。但是我还没有找到导出它的方法。到目前为止,我已经尝试使用 PowerShell 和 Power Automate 自定义 .iqy 文件,但没有成功。

我希望有什么方法可以让我获得完整的历史记录。我需要它来计算平均运行时间。

1 个答案:

答案 0 :(得分:0)

对于 SharePoint Online:

#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"
$ListName="list name"
$CSVFile="C:\VersionHistory.csv"
 
#Call the function to generate version History Report
Export-VersionHistory -SiteURL $SiteURL -ListName $ListName -CSVFile $CSVFile

对于 SharePoint 服务器:

# ******* Variables Section ******************
#Define these variables 
$WebURL="site collection URL"
$ListName ="list name"
$ReportFile = "C:\VersionHistory.csv"
# *********************************************
 
#delete the file if exists
If (Test-Path $ReportFile)
 {
 Remove-Item $ReportFile
 }
 
#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName) 
 
 #Check if list exists
 if($List -ne $null)
 {
  #Get all list items
  $ItemsColl = $List.Items
   
  #Write Report Header
  Add-Content -Path $ReportFile -Value "Item ID, Version Lable, Created by, Created at, Title"
  
  #Loop through each item
  foreach ($item in $ItemsColl) 
  {
      #Iterate each version
      ForEach($version in $item.Versions)
      {
         #Get the version content
         $VersionData = "$($item.id), $($version.VersionLabel), $($version.CreatedBy.User.DisplayName), $($version.Created), $($version['Title'])"
         #Write to report
         Add-Content -Path $ReportFile -Value $VersionData
       }
    }
 }
Write-Host "Version history has been exported successfully!"
相关问题