Поделиться через


How To: Perform Bulk Uploads of Files to SharePoint

This post is going to discuss how to perform a bulk file copy from a folder on the file system to a SharePoint document library. Along with this blog post, I will also provide a downloadable script, which can be modified to work in your environment. As we are copying files from the file system to SharePoint, required metadata may need to be populated via default values or via modification to the script. Additional work could be performed in order to import the metadata from an xml file. This functionality is currently not available in the version of the script provided. This version of the script simply takes exactly what is on the file system and duplicates it in a SharePoint document library.

 

Background

 

I was recently working with a customer who needed to copy files from one SharePoint environment to another SharePoint environment. Although SharePoint does have this functionality built-in via Export-SPWeb and Import-SPWeb, this was not successful in the referenced environment. This was later determined to be related to corruption in the environment.

 

Approach

 

In this case, the approach was simple enough. Read the folder and file structure on the file system and replicate that structure in SharePoint.

 

Solution

 

The solution in this case is a little simpler than what was needed to originally export files from SharePoint to the file system, but the same basic principles apply. What's important in this case is to ensure that we create the folder structure before we start putting files in those folders. In order to do this, we need to be able to separate files from folders. We can use Get-ChildItem in order to accomplish this. Since we are only looking for folders, we can apply a filter.

Once we have a recursive list of all folders, we can loop through this list, creating each folder, and uploading the files in each folder as we go along. Retrieving a list of all files is also achieved by using Get-ChildItem using a slightly different set of filters.

Creating folders is done by using the $SPList.Folders.Add method. When we call this method, we'll have to pass the URL of the folder, the type of object to create, and the name of the folder. We also need to ensure that the folder is published, which can be performed by setting the "_ModerationStatus" property to "0". This is illustrated in the screenshot below.

Uploading Files to SharePoint is also relatively simple using PowerShell. We can use the $SPFolder.folder.files.add method for this. When we call this method, we need to pass a filename, the file stream, and a Boolean of whether or not to overwrite. We then need to perform certain tasks such as checking in, publishing and approving the file. This is illustrated in the screenshot below.

Once completed, the source directory and the destination library should look identical.

 

Download the Script

 

The script can be downloaded from the following location:
BulkFileImport.ps1 (compressed)

 

Usage

 

This script does require some edits. All edits are explained at the top of the script. If you are using multiple source directories and/or multiple destination directories these can be chained together in a single script by inserting multiple iterations of the sample script block. Each source folder and destination library will need to be added to the script using the three lines of PowerShell which are included. Here is a brief explanation.

This line indicates which subfolder within the directory should be used for source files. Do note that this will be appended to the $Directory parameter set higher up in the script:
$SourceFolder = "PowerShell Scripts"

This line indicates which library within the SharePoint site collection the files should be imported to. This parameter should be the title of an existing list in the SharePoint environment:
$DestinationLibrary = $Site.RootWeb.Lists | ? {$_.title -eq "Shared Documents"}

This line calls the ImportFiles function and uses the source folder and the destination library as parameters:
ImportFiles ($Directory + "\" + $SourceFolder) $DestinationLibrary

 

Feedback

 

As always, feedback and suggestions are always welcome. If you do have any ideas on how to improve the script, I'd love to hear them.

You can also follow me on Twitter:

@Rcormier_MSFT

Comments

  • Anonymous
    September 08, 2013
    The comment has been removed

  • Anonymous
    September 16, 2013
    Thank you for this information! http://www.desertedroad.com

  • Anonymous
    December 03, 2013
    Good info! There's another way to upload files in bulk to SharePoint, and add metadata at the same time. Please check www.office365experts.be/.../office-365-bulk-uploader

  • Anonymous
    January 21, 2014
    I am having trouble with the $DestinationLibrary = $Site part of the script.  I am trying to figure out the correct path to put in for the library?  url?  

  • Anonymous
    February 28, 2014
    Anyone modify this for SP Online?

  • Anonymous
    May 18, 2014
    I second it for SP Online. I'm trying to get the snap-in loaded but it's looking like the only way to do that is to run it from a SharePoint server.

  • Anonymous
    February 19, 2015
    @ Everyone asking for O365 Equivalent, feel free to test this one out: gallery.technet.microsoft.com/PowerShell-Bulk-Upload-b9e9d600

  • Anonymous
    April 20, 2015
    is it possible to uplaod bulk documents in to 2013 with the MetaData Tagged,? theere are thoudands of documents whoe meta Data is entered in a Excel Sheet. now this MetaData Needs to be tagged to those douments and uploaded in bulk  to the relavant directories

  • Anonymous
    April 20, 2015
    Yes, it's possible to pass in metadata with a little modification to the script.  There is an example here using a hashtable for metadata: www.sharepointdiary.com/.../bulk-upload-files-to-sharepoint-using-powershell.html Doing it for any field individually would look something like this: $NewItem["Field Name"] = "Field Value"

  • Anonymous
    February 12, 2016
    is there any way to upload files form SharePoint on-Prem to office 365

  • Anonymous
    February 12, 2016
    RPALEU - I would start here: support.office.com/.../SharePoint-Online-Migration-API-User-Guide-555049c6-15ef-45a6-9a1f-a1ef673b867c