|
param( |
|
[string] $listUrl = $null, # = "http://bad.domain.xyz/Path", |
|
[string] $file = $null, # = "C:\SomeFolder\SomeFile.txt", |
|
[string] $checkInComment = "Background Process Upload", |
|
[bool] $publishMajorVersion = $true, |
|
[bool] $approve = $true, # approve it if if approval is required |
|
[string] $approvalComment = "Approved by background process", |
|
[bool] $useFileTimes = $false # use the original file times for SharePoint created/modified |
|
) |
|
|
|
# Original script - 01-May-2013 - David Wise |
|
# GIST Url: https://gist.github.com/DavidWise/5495616 |
|
|
|
# Note: for testing purposes, simply set $listUrl to a valid SharePoint Document Library and $file to the path of a local file to upload and then run the script |
|
|
|
|
|
function SetFileTimes($pUploadedFile, [System.IO.FileInfo] $pLocalFile) { |
|
$item = $pUploadedFile.Item |
|
if ($item -eq $null) { return } |
|
|
|
$item["Created"] = $pLocalFile.CreationTime |
|
$item["Modified"] = $pLocalFile.LastWriteTime |
|
$item.Update() |
|
} |
|
|
|
# basic required parameters |
|
if ($ListUrl -eq $null -or $file -eq $null) { |
|
write-output "A ListUrl and a File must be specified" |
|
exit 4 |
|
} |
|
|
|
# does the specified file exist? |
|
if ([System.IO.File]::Exists($file) -eq $false) { |
|
write-output "The specified file '$file' does not exist" |
|
exit 5 |
|
} |
|
|
|
$destUri = $null |
|
$localFile = get-item -LiteralPath $file |
|
|
|
$site = $null |
|
|
|
try { |
|
# can we open the SharePoint site from the URL? |
|
$destUri = [System.Uri]($listUrl) |
|
write-output "Opening: $listUrl" |
|
$site = [Microsoft.SharePoint.SPSite]($listUrl) |
|
} catch { |
|
$msg = $error[0].Exception.Message |
|
write-output "Unable to open site '$ListUrl' - $msg" |
|
exit 6 |
|
} |
|
|
|
$queryParams = [System.Web.HttpUtility]::ParseQueryString($destUri.Query) |
|
|
|
$folderPath = $queryParams["RootFolder"] |
|
if ($folderPath -eq $null) { |
|
$folderPath = $destUri.LocalPath |
|
} |
|
|
|
$web = $site.OpenWeb() |
|
$folder = $null |
|
$list = $null |
|
$file = $null |
|
$exitCode = 0 |
|
|
|
try { |
|
$folder = $web.GetFolder($folderPath) |
|
|
|
$list = $web.GetList($listUrl) |
|
|
|
$bRequiresCheckIn = $false |
|
|
|
|
|
if ($list -eq $null) { throw "Unable to open list" } |
|
if ($folder -eq $null -or $folder.Exists -eq $false) { throw "Unable to open folder '$folderPath'" } |
|
|
|
#if the library requires a check out, we must do that first |
|
if ($list.ForceCheckout -eq $true) { |
|
$folder.Files | %{ |
|
if ($_.Name -eq $localFile.Name) { |
|
#if we have a match, check it out |
|
# ~~ in testing I found that under certain conditions, the document would exist after deletion but with a version of 0.1 |
|
# ~~ I'm not sure why but turning off the Require CheckOut option and then running this code cleared it up |
|
if ($_.CheckOutType -ne "None") { |
|
$_.UndoCheckOut() |
|
} |
|
$_.CheckOut() |
|
$bRequiresCheckIn = $true |
|
} |
|
} |
|
} |
|
|
|
$stream = $localFile.OpenRead() |
|
$newFile = $folder.Files.Add($folder.Url + "/" + $localFile.Name, $stream, $true, $checkInComment, $false) |
|
$stream.Close() |
|
|
|
if ($useFileTimes) { SetFileTimes $newFile $localFile } |
|
|
|
|
|
if ($bRequiresCheckIn -eq $true -or $publishMajorVersion -eq $true) { |
|
$revType = 0 # minor rev |
|
if ($list.EnableVersioning -eq $true -and $publishMajorVersion -eq $true) { $revType = 1 } |
|
if ($list.EnableVersioning -eq $false) { $revType = 2 } |
|
if ($newFile.CheckOutType -eq "None") { $newFile.CheckOut() } |
|
$newFile.CheckIn($checkInComment, $revType) |
|
} |
|
|
|
if ($list.EnableModeration -eq $true -and $approve -eq $true) { |
|
$newFile.Approve($approvalComment) |
|
} |
|
|
|
Write-output "File Uploaded successfully" |
|
|
|
} catch { |
|
$msg = $error[0].Exception.Message |
|
write-output "Unable to open list Url '$ListUrl' - $msg" |
|
$exitCode=9 |
|
} |
|
finally { |
|
if ($web -ne $null) { $web.Dispose() } |
|
if ($site -ne $null) { $site.Dispose() } |
|
$web = $null |
|
$site = $null |
|
} |
|
|
|
exit $exitCode |
The purpose of this script is obviously to allow a PowerShell script to easily upload a local file into a SharePoint document library. However, in my environment, I never knew what the specific options on that library might be. This script came about as an attempt to address the most common variants, including:
(note: this doesn't work quite right if approval or check in is required as those specifically set the Modified date)
If you run into any issues, please include them in the comments here or on the related post on my blog.