Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
- <#
- .SYNOPSIS
- Adds a file to SharePoint Document Library.
- .DESCRIPTION
- Adds a file to SharePoint Document Library, optionally checks it in, approve it, or overwrite it.
- .PARAMETER FilePath
- The file to Upload
- ex. "c:\Test.docx"
- [String]
- .PARAMETER SPFileName
- The file name to Upload to the SharePoint library.
- Rename the File to something that is SharePoint/URL friendly (e.g. remove special characters "/?=<>" etc.)
- ex. "NewName.docx"
- [String]
- .PARAMETER Web
- The SharePoint web site
- [Microsoft.SharePoint.SPWeb]
- .PARAMETER Library
- The Destination Library on SharePoint
- [String]
- .PARAMETER Approve
- If approval workflow is enabled, when uploading the document, approve the uploaded document
- [bool] Default value = $true
- .PARAMETER CheckIn
- If workflow is enabled, when uploading the document, CheckIn the uploaded document
- [bool] Default value = $true
- .PARAMETER Overwrite
- When uploading the document, if a document with the same name is encountered, overwrite the document?
- [bool] Default value = $false
- .INPUTS
- N/A - Pipeline Inputs
- .OUTPUTS
- SP File
- .EXAMPLE
- $Web = Get-SPWeb "http://MyWebApp/sites/MySite"
- Add-SPFileToLibrary -File "Test.docx" -SPFileName "NewName.docx" -Web $Web -Library "My Document Library" -Approve $true -CheckIn $true -Overwrite $false
- .LINK
- Add-SPFileToLibrary
- #>
- Function Add-SPFileToLibrary {
- [CmdletBinding()]
- Param(
- [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
- [string]$FilePath,
- [Parameter(Position=1, Mandatory=$true)]
- [string]$SPFileName,
- [Parameter(Position=2, Mandatory=$true)]
- [Microsoft.SharePoint.SPWeb]$Web,
- [Parameter(Position=3, Mandatory=$true)]
- [string]$Library,
- [Parameter(Position=4, Mandatory=$true)]
- [bool]$Approve = $true,
- [Parameter(Position=5, Mandatory=$true)]
- [bool]$CheckIn = $true,
- [Parameter(Position=6, Mandatory=$true)]
- [bool]$Overwrite = $false
- ) #end param
- Process
- {
- Try
- {
- # Get Library
- $docLibrary = $web.Lists[$Library]
- $folder = $docLibrary.RootFolder
- # Assign the new document name, and build the destination SharePoint path
- $SPFilePath = ($folder.URL + "/" + $SPFileName)
- #Check if the file exists and the overwrite option is selected before adding the file
- if ((!$web.GetFile($SPFilePath).Exists) -or ($Overwrite))
- {
- $Message = "Uploading... $FilePath ...To... $SPFilePath"
- Write-Debug $Message
- Write-Verbose $Message
- #Support for -WhatIf parameter
- if($PsCmdlet.ShouldProcess($Message))
- {
- Write-Host $Message
- #Upload file
- $File = Get-ChildItem $FilePath
- $spFile = $folder.Files.Add($SPFilePath, $File.OpenRead(), $true) #Upload with overwrite = $true (SP file path, File stream, Overwrite?)
- $spItem = $spFile.Item
- #Check in file to document library (if required)
- #MinorCheckIn=0, MajorCheckIn=1, OverwriteCheckIn=2
- If ($CheckIn) {
- If ($spFile.CheckOutStatus -ne "None") {
- If ($Overwrite){
- #$spFile.CheckIn("File copied from " + $filePath, 2)
- $spFile.CheckIn("Version 1.0", 2)
- }
- Else
- {
- #$spFile.CheckIn("File copied from " + $filePath, 1)
- $spFile.CheckIn("Version 1.0", 1)
- }
- Write-Host "$spfile.Name Checked In"
- }
- } #end CheckIn
- #Approve file (if required)
- If ($Approve)
- {
- If ($spItem.ListItems.List.EnableModeration -eq $true)
- {
- #$spFile.Approve("File automatically approved after copying from " + $filePath)
- $spFile.Approve("Approved")
- If ($spFile.Item["Approval Status"] -eq 0)
- {
- Write-Host "$spfile.Name Approved"
- }
- Else
- {
- Write-Host -Background Yellow -Foreground Black "Warning: $spfile.Name Not Approved"
- }
- }
- } #end Approve
- #Return SP File
- Write-Output $spFile
- }# end upload file
- }
- Else
- {
- If($web.GetFile($SPFilePath).Exists)
- {
- Write-Verbose "File Exists and Overwrite = $false returning file"
- # File Exists and Overwrite = $false, Try finding and returning the file
- $spFile = Get-SPFile -Web $web -Library $Library -SPFileName $SPFileName
- #Return SP File
- Write-Output $spFile
- }
- else
- {
- # File does not exist, and Overwrite = $false, we can't return anything, Throw Warning
- Write-Host -Background Yellow -Foreground Black "Warning: $SPFilePath does not Exist and Overwrite is set to False. No SP File handle returned"
- Write-Output $null
- }
- }# end File Exists and Overwrite Check
- } #end Try
- Catch
- {
- Write-Error $Error[0]
- }
- } #end Process
- } #end Add-SPFileToLibrary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement