Dec15

Synchronize document versions and SharePoint versions with PowerShell

Categories: PowerShell, SharePoint

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

 
 

Links to this post

plant

Trackback from payday loan usage on 21 Oct 2011 07:02

everywhere

Trackback from i need a payday loan on 21 Oct 2011 07:02

title

Trackback from buy cheap diflucan 100mg in Cave creek AZ 85331 Arizona on 21 Oct 2011 07:02

tribunal

Trackback from cialis bestellen online on 21 Oct 2011 07:02

understanding

Trackback from discount drugs viagra on 21 Oct 2011 07:02

wine

Trackback from buy viagra in england in Port graham AK 99603 Alaska on 21 Oct 2011 07:02

attractive

Trackback from 1000 loan payday on 21 Oct 2011 07:02

thought

Trackback from cialis cheapest in White marsh MD 21162 Maryland on 21 Oct 2011 07:02

staircase

Trackback from cialis buy cialis in Watervliet NY 12189 New York (Fishers Is) on 21 Oct 2011 07:02

essay

Trackback from viagra prices baltimore on 21 Oct 2011 07:02

www.torrancepaydayloan.com

Trackback from www.torrancepaydayloan.com on 31 Jan 2012 09:26

Comments

On 25 Aug 2011 07:35, red bottom shoes said:

I found so lots of interestingstuff on your website above all its debate. I am going to register your RSS feed. Carry on the excellent work.

Leave a comment





CAPTCHA Image Validation