to download all files from a SharePoint library
Function Get-FilesFromSPSLibrary
{
Param ( [string] $FolderPath,
[string] $SiteURL,
[string] $ListTitle
)
if (-NOT (Test-Path $FolderPath -PathType 'Container'))
{
write-host "`n[$($FolderPath)] is not a valid folder or you have no access to it`n`n"
return
}
[net.httpWebRequest] $req = [net.webRequest]::create($SiteURL)
$req.Method = "HEAD"
$Req.UseDefaultCredentials = $true;
try {
[net.httpWebResponse] $res = $req.getResponse()
}
Catch {
write-host "`nSite [$SiteURL] is inaccessible`n"
return
}
$MySite = Get-SPWeb -Identity $SiteURL
$Mylist = $MySite.Lists[$ListTitle]
if ($Mylist -eq $null)
{
write-host "Unable to open list [$ListTitle] at [$SiteURL]"
return
}
ForEach ($Fl in $MyList.Items) {
$fname=$Folderpath+$fl.Name
write-host "Downloading [$($Fl.url)] ..."
$binary=$MySite.GetFile($Fl.Url).OpenBinary()
$stream = New-Object System.IO.FileStream($Fname), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
$MySite.Dispose()
}
Get-FilesFromSPSLibrary -FolderPath "C:\TEMP\" -SiteURL "http://sp-test-app:33333" -ListTitle "PowerView Reports"
Get-FilesFromSPSLibrary -FolderPath "C:\TEMP\" -SiteURL "http://sp-test-app:33333" -ListTitle "BISM Connections"
To upload files
Function Put-FilesToSPSLibrary
{
Param ( [string] $FolderPath,
[string] $SiteURL,
[string] $ListTitle
)
if (-NOT (Test-Path $FolderPath))
{
write-host "`n[$($FolderPath)] is not a valid folder or you have no access to it`n`n"
return
}
[net.httpWebRequest] $req = [net.webRequest]::create($SiteURL)
$req.Method = "HEAD"
$Req.UseDefaultCredentials = $true;
try {
[net.httpWebResponse] $res = $req.getResponse()
}
Catch {
write-host "`nSite [$SiteURL] is inaccessible`n"
return
}
$MySite = Get-SPWeb -Identity $SiteURL
$Mylist = $MySite.GetFolder($ListTitle)
if ($Mylist -eq $null)
{
write-host "Unable to open list [$ListTitle] at [$SiteURL]"
return
}
#Get the Files collection
$Files = $MyList.Files
$LocalFiles=Get-Item $Folderpath
if (! $LocalFiles){
write-host "No files in [$FolderPath]. Exisitng ..."
return
}
ForEach ($Fl in $LocalFiles) {
#delete the File from library, if already exist!
if ($Files[$Fl.Name]) {
$Files.delete($ListTitle +"/" + $Fl.Name)
}
write-host "Library:[$ListTitle] - uploading [$($Fl.name)] ..."
#Add File to the collection
$rc = $Files.Add($ListTitle +"/" + $Fl.Name,$Fl.OpenRead(),$false)
}
$MySite.Dispose()
}
Put-FilesToSPSLibrary -FolderPath "C:\TEMP\*.BISM" -SiteURL "http://sp-test-app:33333" -ListTitle "upload bism connections"
Put-FilesToSPSLibrary -FolderPath "C:\TEMP\*.RDLX" -SiteURL "http://sp-test-app:33333" -ListTitle "Upload PV reports"
Function Get-FilesFromSPSLibrary
{
Param ( [string] $FolderPath,
[string] $SiteURL,
[string] $ListTitle
)
if (-NOT (Test-Path $FolderPath -PathType 'Container'))
{
write-host "`n[$($FolderPath)] is not a valid folder or you have no access to it`n`n"
return
}
[net.httpWebRequest] $req = [net.webRequest]::create($SiteURL)
$req.Method = "HEAD"
$Req.UseDefaultCredentials = $true;
try {
[net.httpWebResponse] $res = $req.getResponse()
}
Catch {
write-host "`nSite [$SiteURL] is inaccessible`n"
return
}
$MySite = Get-SPWeb -Identity $SiteURL
$Mylist = $MySite.Lists[$ListTitle]
if ($Mylist -eq $null)
{
write-host "Unable to open list [$ListTitle] at [$SiteURL]"
return
}
ForEach ($Fl in $MyList.Items) {
$fname=$Folderpath+$fl.Name
write-host "Downloading [$($Fl.url)] ..."
$binary=$MySite.GetFile($Fl.Url).OpenBinary()
$stream = New-Object System.IO.FileStream($Fname), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
$MySite.Dispose()
}
Get-FilesFromSPSLibrary -FolderPath "C:\TEMP\" -SiteURL "http://sp-test-app:33333" -ListTitle "PowerView Reports"
Get-FilesFromSPSLibrary -FolderPath "C:\TEMP\" -SiteURL "http://sp-test-app:33333" -ListTitle "BISM Connections"
To upload files
Function Put-FilesToSPSLibrary
{
Param ( [string] $FolderPath,
[string] $SiteURL,
[string] $ListTitle
)
if (-NOT (Test-Path $FolderPath))
{
write-host "`n[$($FolderPath)] is not a valid folder or you have no access to it`n`n"
return
}
[net.httpWebRequest] $req = [net.webRequest]::create($SiteURL)
$req.Method = "HEAD"
$Req.UseDefaultCredentials = $true;
try {
[net.httpWebResponse] $res = $req.getResponse()
}
Catch {
write-host "`nSite [$SiteURL] is inaccessible`n"
return
}
$MySite = Get-SPWeb -Identity $SiteURL
$Mylist = $MySite.GetFolder($ListTitle)
if ($Mylist -eq $null)
{
write-host "Unable to open list [$ListTitle] at [$SiteURL]"
return
}
#Get the Files collection
$Files = $MyList.Files
$LocalFiles=Get-Item $Folderpath
if (! $LocalFiles){
write-host "No files in [$FolderPath]. Exisitng ..."
return
}
ForEach ($Fl in $LocalFiles) {
#delete the File from library, if already exist!
if ($Files[$Fl.Name]) {
$Files.delete($ListTitle +"/" + $Fl.Name)
}
write-host "Library:[$ListTitle] - uploading [$($Fl.name)] ..."
#Add File to the collection
$rc = $Files.Add($ListTitle +"/" + $Fl.Name,$Fl.OpenRead(),$false)
}
$MySite.Dispose()
}
Put-FilesToSPSLibrary -FolderPath "C:\TEMP\*.BISM" -SiteURL "http://sp-test-app:33333" -ListTitle "upload bism connections"
Put-FilesToSPSLibrary -FolderPath "C:\TEMP\*.RDLX" -SiteURL "http://sp-test-app:33333" -ListTitle "Upload PV reports"
I am trying to do this with Sharepoint online.
ReplyDeleteI've managed to authenticate but the write only writes 1 byte
any tips would be helpful