Uploading reports (.csv) to a SharePoint site.


This is something that I run into very often.  Administrators running scripts that provide insight into their SharePoint environment.
These scripts often generate a report of some format; and they also want them uploaded to a document library; to finish off the automation process.

Below is a Cmdlet/ function that I always suggest customers add in just for this purpose:

function Upload-ReportFile
{
 param
 (
  [parameter(mandatory=$true)]
  [string]$LocalReportPath,
  [parameter(mandatory=$true)]
  [string]$LocalReportFileName,
  [parameter(mandatory=$true)]
  [string]$DestinationSiteUrl,
  [parameter(mandatory=$true)]
  [string]$DestinationDocumentLibrary,
 )
 Write-Debug "Begin: Get destination web object"
 $reportDestination = Get-SPWeb -Identity $DestinationSiteUrl
 Write-Debug "End: Get destination web object // found $reportDestination"
 Write-Debug "Begin: Check for document library in destination site"
 $reportTarget = $reportDestination.Lists.TryGetList($DestinationDocumentLibrary)
 Write-Debug "Begin: Attempting to upload report to document library" 
 $localReport = Get-Item -Path "$LocalReportPath\$LocalReportFileName"
 Write-Debug "Begin: Checking if local report can be found // found $localReport"
 
 if (!$localReport)
 {
  Write-Host "The report that you are trying to upload does not exist or was not found." 
 }
 Write-Debug "End: Checking if local report can be found"
 Write-Debug "Begin: Uploading file now!"
 $reportStream = $localReport.OpenRead()
 $reportAdd = $reportTarget.RootFolder.Files.Add($localReport.Name, [System.IO.FileStream]$reportStream, $true)
 $reportStream.Close()
 $reportTarget.Update()
 Write-Debug "End: Attempting to upload report to document library"
}

Have fun
Steve (@moss_sjeffery)

Comments (0)

Skip to main content