Advertisement
kminchau

Add-SPFileToLibrary - Add a file to SharePoint Library

Jun 10th, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
  2.  
  3. <#
  4. .SYNOPSIS
  5.     Adds a file to SharePoint Document Library.
  6. .DESCRIPTION
  7.     Adds a file to SharePoint Document Library, optionally checks it in, approve it, or overwrite it.
  8. .PARAMETER FilePath
  9.     The file to Upload
  10.     ex. "c:\Test.docx"
  11.     [String]
  12. .PARAMETER SPFileName
  13.     The file name to Upload to the SharePoint library.
  14.     Rename the File to something that is SharePoint/URL friendly (e.g. remove special characters "/?=<>" etc.)
  15.     ex. "NewName.docx"
  16.     [String]
  17. .PARAMETER Web
  18.     The SharePoint web site
  19.     [Microsoft.SharePoint.SPWeb]
  20. .PARAMETER Library
  21.     The Destination Library on SharePoint
  22.     [String]
  23. .PARAMETER Approve
  24.     If approval workflow is enabled, when uploading the document, approve the uploaded document
  25.     [bool] Default value = $true
  26. .PARAMETER CheckIn
  27.     If workflow is enabled, when uploading the document, CheckIn the uploaded document
  28.     [bool] Default value = $true
  29. .PARAMETER Overwrite
  30.     When uploading the document, if a document with the same name is encountered, overwrite the document?
  31.     [bool] Default value = $false
  32. .INPUTS
  33.     N/A - Pipeline Inputs
  34. .OUTPUTS
  35.     SP File
  36. .EXAMPLE
  37.     $Web = Get-SPWeb "http://MyWebApp/sites/MySite"
  38.     Add-SPFileToLibrary -File "Test.docx" -SPFileName "NewName.docx" -Web $Web -Library "My Document Library" -Approve $true -CheckIn $true -Overwrite $false
  39. .LINK
  40.     Add-SPFileToLibrary
  41. #>
  42. Function Add-SPFileToLibrary {
  43.     [CmdletBinding()]
  44.     Param(
  45.         [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  46.             [string]$FilePath,
  47.         [Parameter(Position=1, Mandatory=$true)]
  48.             [string]$SPFileName,
  49.         [Parameter(Position=2, Mandatory=$true)]
  50.             [Microsoft.SharePoint.SPWeb]$Web,
  51.         [Parameter(Position=3, Mandatory=$true)]
  52.             [string]$Library,
  53.         [Parameter(Position=4, Mandatory=$true)]
  54.             [bool]$Approve = $true,
  55.         [Parameter(Position=5, Mandatory=$true)]
  56.             [bool]$CheckIn = $true,
  57.         [Parameter(Position=6, Mandatory=$true)]
  58.             [bool]$Overwrite = $false
  59.     ) #end param
  60.     Process
  61.     {
  62.         Try
  63.         {
  64.             # Get Library
  65.             $docLibrary = $web.Lists[$Library]
  66.             $folder = $docLibrary.RootFolder
  67.  
  68.             # Assign the new document name, and build the destination SharePoint path
  69.             $SPFilePath = ($folder.URL + "/" + $SPFileName)
  70.  
  71.             #Check if the file exists and the overwrite option is selected before adding the file
  72.             if ((!$web.GetFile($SPFilePath).Exists) -or ($Overwrite))
  73.             {
  74.                 $Message = "Uploading...   $FilePath   ...To...   $SPFilePath"
  75.                 Write-Debug $Message
  76.                 Write-Verbose $Message
  77.  
  78.                 #Support for -WhatIf parameter
  79.                 if($PsCmdlet.ShouldProcess($Message))
  80.                 {
  81.                     Write-Host $Message
  82.  
  83.                     #Upload file
  84.                     $File = Get-ChildItem $FilePath
  85.                     $spFile = $folder.Files.Add($SPFilePath, $File.OpenRead(), $true)  #Upload with overwrite = $true (SP file path, File stream, Overwrite?)
  86.  
  87.                     $spItem = $spFile.Item
  88.  
  89.                     #Check in file to document library (if required)
  90.                     #MinorCheckIn=0, MajorCheckIn=1, OverwriteCheckIn=2
  91.                     If ($CheckIn) {
  92.                         If ($spFile.CheckOutStatus -ne "None") {
  93.                             If ($Overwrite){
  94.                                 #$spFile.CheckIn("File copied from " + $filePath, 2)
  95.                                 $spFile.CheckIn("Version 1.0", 2)
  96.                             }
  97.                             Else
  98.                             {
  99.                                 #$spFile.CheckIn("File copied from " + $filePath, 1)
  100.                                 $spFile.CheckIn("Version 1.0", 1)
  101.                             }
  102.                             Write-Host "$spfile.Name Checked In"
  103.                         }
  104.                     } #end CheckIn
  105.  
  106.                     #Approve file (if required)
  107.                     If ($Approve)
  108.                     {
  109.                         If ($spItem.ListItems.List.EnableModeration -eq $true)
  110.                         {
  111.                             #$spFile.Approve("File automatically approved after copying from " + $filePath)
  112.                             $spFile.Approve("Approved")
  113.                             If ($spFile.Item["Approval Status"] -eq 0)
  114.                             {
  115.                                 Write-Host "$spfile.Name Approved"
  116.                             }
  117.                             Else
  118.                             {
  119.                                 Write-Host -Background Yellow -Foreground Black "Warning: $spfile.Name Not Approved"
  120.                             }
  121.                         }
  122.                     } #end Approve
  123.  
  124.                     #Return SP File
  125.                     Write-Output $spFile
  126.  
  127.                 }# end upload file
  128.             }
  129.             Else
  130.             {
  131.                 If($web.GetFile($SPFilePath).Exists)
  132.                 {
  133.                     Write-Verbose "File Exists and Overwrite = $false returning file"
  134.                     # File Exists and Overwrite = $false, Try finding and returning the file
  135.                     $spFile = Get-SPFile -Web $web -Library $Library -SPFileName $SPFileName
  136.  
  137.                     #Return SP File
  138.                     Write-Output $spFile
  139.                 }
  140.                 else
  141.                 {
  142.                     # File does not exist, and Overwrite = $false, we can't return anything, Throw Warning
  143.                     Write-Host -Background Yellow -Foreground Black "Warning: $SPFilePath does not Exist and Overwrite is set to False. No SP File handle returned"
  144.                     Write-Output $null
  145.                 }
  146.             }# end File Exists and Overwrite Check
  147.  
  148.         } #end Try
  149.         Catch
  150.         {
  151.             Write-Error $Error[0]
  152.         }
  153.     } #end Process
  154. } #end Add-SPFileToLibrary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement