【问题标题】:Export version history event log of SharePoint List导出 SharePoint 列表的版本历史事件日志
【发布时间】:2021-07-30 14:47:19
【问题描述】:

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

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

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

现在我“只”需要在表格或文件(xlsx、xml、json、csv)中构建这些数据。并且对于 List 的所有项目。

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

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

【问题讨论】:

    标签: sharepoint


    【解决方案1】:

    对于 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 Server:

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

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 2019-10-07
      • 2011-08-10
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多