Upload File to web part gallery

19/07/2012 22:31

 

$srcPath="c:\temp"
$siteUrl="http://localhost/sites/events"
$libraryFolder="_catalogs/wp"
$libraryUrl=$siteUrl + "/" + $libraryFolder
 
 
function UploadFile
{
    param([string]$srcPath,
            [string]$fileName,
            [string]$libraryUrl,
            [string]$libraryFolder)
   
  try
  {         
  $web=get-spweb $siteUrl
  
  $folder=$web.GetFolder($libraryFolder)         
  $file=get-childitem ($srcPath + "\" + $fileName)
  $files=$folder.Files
  $files.Add(($libraryUrl+"/" + $fileName), $file.OpenRead(), $true)
    
  #check in only when files are checked out, when it's publishing page
  if($files[$fileName].checkOutStatus -ne "None")
  {
    $files[$fileName].checkIn("checked in by Powershell")
    $files[$fileName].publish("published by Powershell")
    start-sleep -s 5
    $files[$fileName].Item.File.Approve("Approved by Powershell")
  }
  }
  catch [Exception]
  {
      write-error $_
  }
  finally
  {
  if($web -eq $null)
    {
    $web.dispose()
    }
    
  }           
}
 
$files=get-childitem $srcPath
 
foreach($file in $files)
{
    UploadFile -srcPath $srcPath -fileName $file.Name -libraryUrl $libraryUrl -libraryFolder $libraryFolder
}