How can we read version history of document library and update particular version (few fileds) data in Sqlserver database?
Question
I want to read version history of document library.
and update few fields data of a particular version history in sqlserver table.
Any suggestions would be much appreciated.
Thanks in advance.
Solution
To get the versions history
fields via SQL
- Open SQl Management Studio > Connect to your SharePoint server.
- Navigate to Content database like
[WSS_Content]
. - Create a new query from > right click Design Query in Editor > Select
[dbo].[AllDocVersions]
tableinner join
[AllDocs]
table on DocID
, select your required fields to show.
the Query should look like with your required fields
SELECT AllDocVersions.Id, AllDocVersions.UIVersion, AllDocs.DirName, AllDocs.LeafName
FROM AllDocs INNER JOIN
AllDocVersions ON AllDocs.Id = AllDocVersions.Id
Output:
Note:
Microsoft does not support any changes from the database , so it's heavily recommended to avoid any changes via the database.
[update]
Below is a PowerShell code which can be used to export version history to Excel Ref
# ******* Variables Section ******************
#Define these variables
$WebURL="http://sharepoint.crescent.com/sites/Sales/"
$ListName ="Invoice"
$ReportFile = "D:\Invoice_VersionHistory.csv"
# *********************************************
#delete 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!"
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange