How to Stream Files between Site Collections and Sites in SharePoint with JavaScript

Some time ago I was working on a client-side migration tool to use in SharePoint Online – to copy files and their metadata between libraries both within the same site collection, and into different site collections. It turned out to be a pretty difficult problem – but was solved in the end by using the browser itself as a lifeboat to carry the data between the site collections. If you’re reading this post you probably already looked at the Copy webservice, and “CopyIntoItemsLocal”, which only works when the source and destination are in the same site collection (and causes trouble by populating the hidden “copysource” metadata of the file in the destination file).


Along the way, I discovered a very nice jQuery extension called SPServices, which neatly wrapped up some of the SharePoint API commands, and made the development much more straightforward than it might otherwise have been. The snippet below uses SPServices to load a document within SharePoint into the browser’s memory, and then to write it back into a file elsewhere in SharePoint.

var source_file_url = "https://server/sites/site_collection_a/library_a/document_a.docx";
var destination_file_url = "https://sites/site_collection_b/library_b/document_b.docx";

setTimeout(function() {
    var streamLength = 0;
    // Read the SourceFileURL into memory
        operation: "GetItem",
        Url: source_file_url,
        async: false,
        completefunc: function (xData, Status) {
            itemstream = $(xData.responseXML).find("Stream").text();
            streamLength = itemstream.length;
            itemfields = "";
    if (streamLength) {
        // Write the data back into the DestinationFileURL
            operation: "CopyIntoItems",
            SourceUrl: source_file_url,
            async: false,
            DestinationUrls: [destination_file_url],
            Stream: itemstream,
            completefunc: function (xData, Status) {
                // file has copied at this point
    else {
        // failed to find list content type
}, 0);

It’s worth noting that at the point the file has been copied, you probably still need to do some work around setting content types (if that is important to you) – I will cover this when I get a chance to write it up, because it’s NOT trivial (especially doing everything on the client-side).

It’s also worth noting that the snippet above is wrapped in a setTimeout call – a handy trick I discovered with asynchronous calls in JavaScript to make sure the code within completes before anything else happens. Kind of a cheap alternative to promises.

Posted by Jonathan Beckett in Notes, 0 comments

Bulk Uploading Files into Libraries in SharePoint with PowerShell

The following snippet shows a method of uploading a folder of files on the filesystem into a library in SharePoint. In this case the filesystem folder is presumed to exist in the same location as the PowerShell script, called “SiteAssets”, uploading to a library in the SharePoint subsite called “SiteAssets”.

# Connect to SharePoint Site
$web = get-spweb "https://server/sites/site_collection/subsite"

# loop through all files in the local siteassets subfolder
foreach ( $source_file in $(Get-ChildItem './SiteAssets' | Sort-Object -Property Name) ) {
    if ($source_file.Attributes -match 'Directory') {
        # its a folder - ignore it
    } else {
        $library = $web.GetFolder("SiteAssets")
        $library_files = $library.Files

        $file = Get-ChildItem $source_file.FullName

        $library_files.Add("SiteAssets/" + $source_file.Name,$file.OpenRead(),$true) > $null

# Release resources
Posted by Jonathan Beckett in Notes, 0 comments