Download Document Version History from SharePoint in SSIS

27 June 2022
KingswaySoft Team

SharePoint Document Library supports version histories so that you can store and track different versions of each document file within the library as changes are made to the file. Such version control infrastructure provides many unique benefits. For instance, if something went wrong with one of the files because of a recent accidental/unintended change, you can easily restore to a prior version as needed. In your ETL development, you might often run into the requirements that you need to extract all previous versions of a file. In this blog post, we will show how you could achieve so using our SharePoint Source component to download document library file versions and save them to the local file system using the Premium File System Destination component (offered in SSIS Productivity Pack) in SSIS.

To retrieve the version histories of a document library in SharePoint, you need to enable the “Download Version History” option in the SharePoint Source component, where you can also specify the maximum number of versions to retrieve for each document. In our example, we are retrieving all versions by setting up the property to 0.

Image 001 - SharePoint Source Component

Once the “Download Version History” option has been enabled, a secondary output will be added to the Source Component called Version History (Document Library). Within the new output, you will be able to find all columns related to SharePoint document versions.

Image 002 - SharePoint Source Version History Output

As a general best practice, you should only select those columns/fields that are needed for the downstream pipeline components. As shown in the screenshot above, only “File Content”, “Url” and “Version Number” fields are selected in the Version History output.

Once the SharePoint Source component has been configured, we use a Premium Derived Column component before writing the version histories files to the local system, which uses the following expression to generate the FileName and specify the target parent folder path.

The SharePoint document name can be extracted from the “Url” output column using a Derived Column expression like below:

GetTokenAtPosition( [Url], "/", GetTokenCount( [Url],"/" ))

Given a URL value like “https://{sites}.sharepoint.com/sites/TestSite/DocumentLibrary/Test File.txt”, the output of the above expression is “Test File.txt”, which is the original SharePoint document name.

In order to properly name each downloaded version, we utilize the following expression so that we create a unique name for each file version:

GetTokenAtPosition( GetTokenAtPosition( [Url], "/", GetTokenCount( [Url],"/" )), ".",1 )+"_"+[Version]+"."+GetTokenAtPosition( GetTokenAtPosition( [Url], "/",  GetTokenCount( [Url],"/" )), ".",2 )
For instance, if a file name is “Test File.txt”, the versioned file names will be “Test File_1.0.txt”, “Test File_2.0.txt”, and so on.

Image 003 - Premium Derived Column Functions

A hard-coded target path is also specified in the Premium Derived Column in our example.

Lastly, we write the document library file versions to the local file system using the Premium File System Destination component. The overall data flow design of the package is shown in the screenshot below:

Image 004 - Data Flow Design

By using the above design, you now have an SSIS package that can download SharePoint Document Library version histories and save all files along with their historical versions to a local file system. The design can be further expanded or tailored to suit your unique business requirements, for instance, you can save such document version histories to a different cloud storage space like Amazon S3/Azure Blob Storage/OneDrive using our cloud storage destination components.

We hope you find this blog post helpful.

Archive

Tags