Imagine you import some documents from file system to sharepoint. You may experience that your former document versions do not match the item versions created by sharepoint. Since the version field is readonly, it is not possible for you to manipulate that value by default.
In this article, I´ll show you one possibility (there´s many) how you can solve that kind of problem. To keep it short, I decided to choose a "no build" approach and just do it with a tiny little helper called PowerShell (PoSH)! That means there´s NO visual studio involved and NO deployment is necessary to get this done.
Before we start scripting we should investigate on the way how SharePoint is handling and managing versions internally. Do to this, activated versioning on list-level is mandatory:
If we take a look at a version history inside the PoSH, we recognize a certain but unexpected pattern (at a first glance) and two relevant fields:
VersionId: 512
VersionLabel: 1.0
If you process updates on an item, the output will change as follows:
VersionId: 513
VersionLabel: 1.1
You can approve the item, and the output will be:
VersionId: 1024
VersionLabel: 2.0
You see, there´s a simple formula which we have to keep in consideration later in the script:
VersionId = (MainVersion * 512) + Subversion
Next we need to add a field to our list to store the former document version of the file system:
Name: ImportVersion, Type: Text
Now we can finally open up the PoSH. If you are working with PowerShell and SharePoint, I recommend you to modify your profile as described on iLoveSharepoint
The script is pretty straight forward though:
$web = get-spweb http://localhost/websites/spbombshell
$list = $web.Lists["ListName"]
$item = $list.Items[0]
$importVersion = [int]$item["ImportVersion"]
$itemVersion = [int]($item.Versions[0].VersionId / 512)
$count = $importVersion - $itemVersion
for ($index = 0; $index -lt $count; $index++) {
$item.File.CheckOut()
$item.File.CheckIn("PoSH Check-In")
$item.File.Approve("PoSH Approve")
}
Check back the version history of the modified item:
We must get rid of the history lines, so we add one more line to our script:
$item.File.Versions.RecycleAll()
That´s it – your versions are synchronized.
By Markus Alt
Technorati-Tags: Sharepoint,Powershell