1

In my local file system I have a document with 3 others considering them as major versions of the main copy.

Main.docx
 Ver3_OfMain.docx
 Ver2_OfMain.docx
 Ver1_OfMain.docx

I would want to have a PowerShell script, which creates/copy this Main.docx on SharePoint Online Document Library.

Where Ver3_OfMain.docx, Ver2_OfMain.docx, Ver1_OfMain.docx as the major versions of Main.docx

2 Answers 2

5

You cannot randomly access/modify/create versions in SharePoint. So, your only option is to enable (major) versioning on the doc lib, and then upload versions in the correct order (from Ver1_OfMain.docx to Main.docx).
At each upload (even if made by code/PowerShell), SharePoint will automatically increment the version counter for that doc. Obvisouly, you'll have to rename versioned files so the name is always the same (e.g. Main.docx).

+How to upload files into SPO from PowerShell: http://blogs.technet.com/b/fromthefield/archive/2014/02/19/office365-script-to-upload-files-to-a-document-library-using-csom.aspx

3
  • So naming all files as Main.docx, If we upload all files then each will be considered as a version? Correct me if I am wrong.
    – Asad Refai
    Sep 8, 2015 at 11:26
  • You're right! :) Assuming major versioning is enabled on the doc lib.
    – Evariste
    Sep 8, 2015 at 11:30
  • Cheers mate, I have prepared a function by your given link and it worked.
    – Asad Refai
    Sep 8, 2015 at 12:55
4

From a brilliant answer by @Evariste, I have prepared a PowerShell code to migrate documents from file system to SPO Doc Lib with versions.

Pre requisite:

  • The Document library must be enabled with Versioning

Consider following example, where I have a Document with 3 versions. Here we need to come up with a naming convention so that its easy for us to judge which is the latest one.

Main_1.docx
Main_2.docx
Main_3.docx
Main_4.docx

So its clear Main_1.docx was the first version and Main_4.docx is the latest

Note: In file system they have different names, but these must be saved with similar name then and only then versions will be created.

Below is the PowerShell script I have used to create documents with its versions on SPO Document library.

#Specify tenant admin and site URL
$User = "[email protected]"
$SiteURL = "https://tenant.sharepoint.com"
$Folder = "D:\Version"
$DocLibName = "VersionCheck"

#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

#Upload file
Foreach ($File in (dir $Folder -File))
{
    # Generate file's name.
    #
    $TempFileName = $File.BaseName.Split("_")


    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    #$FileCreationInfo.URL = $File

    # Here we replace different names by one single. i.e. in each iteration file name is Main_1, Main_2 etc.. $TempFileName[0] will give us common name which is Main
    #
    $FileCreationInfo.URL = $TempFileName[0] + $File.Extension
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    $Context.Load($Upload)
    $Context.ExecuteQuery()

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.