SPArchitect.Write()

Random SharePoint Musings

Uploading Documents to SharePoint via PowerShell

leave a comment »

The following PowerShell functions can be used to upload one or more documents to SharePoint Document Libraries using an instance of a System.Net.WebClient. The WebClient (think WebDAV) can handle uploading files w/o having to mess with byte streams and whatnot.

The first function, Upload-Document, will upload one or more files (piped in) to the URL specified. Here’s the function:

function Upload-Document([string]$baseUrl){

$wc = New-Object System.Net.WebClient

$wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$input | % {

# Don't attempt to 'upload' folders, only files inside them

if($_.Length -ne $null){

$fp = $_.FullName

$d = $_.Name

$p = split-path -parent $fp

$f = split-path -leaf $p

$targetUrl = "$baseUrl/$f/$d"

Write-Host "Uploading $_ to $baseUrl/$f..." –NoNewline

$wc.UploadFile($targetUrl, "PUT", "$fp")

Write-Host "Done!"

}

}

}

(% is an alias for the ForEach-Object cmdlet)

The function is written in such a way that it will attempt to map the folder structure to the URL from the parent folder down. That doesn’t sound very clear when I read it aloud, so perhaps some samples will help:

PS C:\Users\SPAdmin\Documents> Get-Item .\test.docx | Upload-Document http://my.sharepoint.site

This will upload the file text.docx to the /documents library at http://my.sharepoint.site because the local folder that the file lives in is named Documents.

PS C:\Users\SPAdmin\Documents> Get-Item .\Requirements\test.docx | Upload-Document http://my.sharepoint.site/documents

This will upload the file test.docx to the /requirements folder inside the /documents library because the URL specified includes the library URL.

NOTE: These functions will NOT automatically create folders in a Document Library.

You could also pipe in the output from a dir command to do multiple files matching a pattern:

PS C:\Users\SPAdmin\Documents> dir .\*.docx | Upload-Document http://my.sharepoint.site

This one will upload any .docx files in the current folder (Documents) to the /documents library on the site. This is the method used by the next function, Upload-Documents.

Upload-Documents is the one you’re more likely to use. It is designed such that you can point it at a folder that contains a subfolder for each library on the site, said subfolders containing the documents you want uploaded. Here’s the function:

function Upload-Documents([string]$baseUrl, $rootFolder){

dir $rootFolder -recurse | Upload-Document $baseUrl

}

In my Deploy folder for the project, I have a folder named Content. Inside that folder are two subfolders named Documents and TrainingDocuments, which map to libraries on the site. Executing the following will upload all of the documents to the matching libraries on the site:

PS C:\Projects\MySharePointSite\Deploy> Upload-Documents http://my.sharepoint.site .\Content

Remember, when using this function, the subfolders of .\Content need to be named the same as the libraries (URLs) on the site where you want the documents to be “PUT”

Advertisements

Written by Jim Duncan

August 15, 2011 at 9:54 am

Posted in PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: