Created
August 24, 2014 19:50
-
-
Save vgrem/26bf79747665687feb2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | |
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | |
. ".\Invoke-RestSPO.ps1" | |
<# | |
.SYNOPSIS | |
Retieve Files in Folder | |
.DESCRIPTION | |
Read Files operation via SharePoint 2013 REST API | |
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files | |
method: GET | |
headers: | |
Authorization: "Bearer " + accessToken | |
accept: "application/json;odata=verbose" or "application/atom+xml" | |
.NOTES | |
Prerequisite : Invoke-RestSPO function | |
.EXAMPLE | |
$Files = Get-SPOFiles -WebUrl $WebUrl -UserName $UserName -Password $Password -FolderUrl '/Shared Documents/Folder' | |
#> | |
Function Get-SPOFiles(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$FolderUrl | |
) | |
$Url = $WebUrl + "/_api/web/GetFolderByServerRelativeUrl('" + $FolderUrl + "')/Files" | |
Invoke-RestSPO $Url Get $UserName $Password | % { $_.results } | |
} | |
<# | |
.SYNOPSIS | |
Delete File | |
.DESCRIPTION | |
Delete Files operation via SharePoint 2013 REST API | |
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name') | |
method: POST | |
headers: | |
Authorization: "Bearer " + accessToken | |
X-RequestDigest: form digest value | |
IF-MATCH: etag or "*" | |
X-HTTP-Method:"DELETE" | |
.NOTES | |
Prerequisite : Invoke-RestSPO function | |
.EXAMPLE | |
Delete-SPOFile -WebUrl $WebUrl -UserName $UserName -Password $Password -FileUrl '/Shared Documents/Folder/File To Delete' | |
#> | |
Function Delete-SPOFile(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$FileUrl | |
) | |
$Url = $WebUrl + "/_api/web/GetFileByServerRelativeUrl('" + $FileUrl + "')" | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO -Url $Url -Method Post -UserName $UserName -Password $Password -RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue -ETag "*" -XHTTPMethod "DELETE" | |
} | |
<# | |
.SYNOPSIS | |
Download File | |
.DESCRIPTION | |
Read File operation via SharePoint 2013 REST API | |
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/$value | |
method: GET | |
headers: | |
Authorization: "Bearer " + accessToken | |
.NOTES | |
Prerequisite : Invoke-RestSPO function | |
.EXAMPLE | |
Download-SPOFile -WebUrl $WebUrl -UserName $UserName -Password $Password -FileUrl '/Shared Documents/Folder/File To Download' -DownloadPath 'c:\downloads' | |
#> | |
Function Download-SPOFile(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$True)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$FileUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$DownloadPath | |
) | |
$Url = $WebUrl + "/_api/web/GetFileByServerRelativeUrl('" + $FileUrl + "')/`$value" | |
$fileContent = Invoke-RestSPO -Url $Url -Method Get -UserName $UserName -Password $Password -BinaryStringResponseBody $True | |
#Save | |
$fileName = [System.IO.Path]::GetFileName($FileUrl) | |
$downloadFilePath = [System.IO.Path]::Combine($DownloadPath,$fileName) | |
[System.IO.File]::WriteAllBytes($downloadFilePath,$fileContent) | |
} | |
<# | |
.SYNOPSIS | |
Upload File | |
.DESCRIPTION | |
Create File operation via SharePoint 2013 REST API | |
url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name') | |
method: POST | |
headers: | |
Authorization: "Bearer " + accessToken | |
X-RequestDigest: form digest value | |
IF-MATCH: etag or "*" | |
X-HTTP-Method:"DELETE" | |
.NOTES | |
Prerequisite : Invoke-RestSPO function | |
.EXAMPLE | |
Upload-SPOFile -WebUrl $WebUrl -UserName $UserName -Password $Password -FolderUrl '/Shared Documents/Folder' -UploadFilePath 'Physical Path to File' | |
#> | |
Function Upload-SPOFile(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$True)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$FolderUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UploadFilePath | |
) | |
$FileInfo = New-Object System.IO.FileInfo($UploadFilePath) | |
$Url = $WebUrl + "/_api/web/GetFolderByServerRelativeUrl('" + $FolderUrl + "')/Files/add(url='" + $FileInfo.Name + "',overwrite=true)" | |
$FileContent = [System.IO.File]::ReadAllBytes($FileInfo.FullName) | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO -Url $Url -Method Post -UserName $UserName -Password $Password -Body $FileContent -RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment