Download webpart file from web part gallery

19/07/2012 22:17


#input parameters
$siteUrl="https://localhost/sites/events"
$destination="c:\temp"
function DownloadWPfile
{
    param([string]$siteUrl,
            [string]$fileName,
            [string]$destination)
      
      try
      {
       $web=get-spweb $siteUrl
 $folder=$web.GetFolder("_catalogs/wp")        
 $files=$folder.Files
      $file=$files[$fileName]         
  
     #Ensure destination directory
        $destinationfolder = $destination # + "/" + $folder.Url
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }
   
 #check in only when files are checked out, when it's publishing site
     
   
       write-host "Downloading " $file.Name "..."
   #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
    write-host "Downloading completed."
         }
         catch [Exception]
         {
            write-error $_
         }
      
      
 }
DownloadWPfile -siteUrl $siteUrl -fileName "chamEventsSprekers.webpart" -destination $destination