Overview
In the previous post we’ve already discussed how to perform CRUD operations by sending HTTPS requests to SharePoint RESTful web services in PoweShell. The Invoke-RestSPO function was introduced for that purpose since Invoke-RestMethod cmdlet does not support claims based authentication and it makes this cmdlet impossible to use in O365 and SharePoint Online scenarios.
This time I am going to demonstrate how to perform basic create, read, update, and delete (CRUD) operations on folders and files with the SharePoint 2013 REST interface using Invoke-RestSPO function.
Working with folders
Folder resource: represents a folder on a SharePoint Web site
Endpoint URI: http://<site url>/_api/web/getfolderbyserverrelativeurl(‘/<folder name>‘)
Supported HTTP methods: GET | POST | DELETE | MERGE | PUT
The following examples demonstrates how to perform basic CRUD operations with Folder resource.
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 Folder | |
.DESCRIPTION | |
Read Folder operation via SharePoint 2013 REST API | |
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents') | |
method: GET | |
headers: | |
Authorization: "Bearer " + accessToken | |
accept: "application/json;odata=verbose" or "application/atom+xml" | |
.NOTES | |
Prerequisite : Invoke-RestSPO function | |
.EXAMPLE | |
$Folder = Get-SPOFolder -WebUrl $WebUrl -UserName $UserName -Password $Password -FolderUrl '/Shared Documents/Folder To Read' | |
#> | |
Function Get-SPOFolder(){ | |
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 + "')" | |
Invoke-RestSPO $Url Get $UserName $Password | |
} | |
<# | |
.SYNOPSIS | |
Create Folder | |
.DESCRIPTION | |
Create Folder operation via SharePoint 2013 REST API. | |
url: http://site url/_api/web/folders | |
method: POST | |
body: { '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/document library relative url/folder name'} | |
Headers: | |
Authorization: "Bearer " + accessToken | |
X-RequestDigest: form digest value | |
accept: "application/json;odata=verbose" | |
content-type: "application/json;odata=verbose" | |
content-length:length of post body | |
.NOTES | |
Prerequisite : Invoke-RestSPO function | |
.EXAMPLE | |
$Folder = Create-SPOFolder -WebUrl $WebUrl -UserName $UserName -Password $Password -FolderUrl '/Shared Documents/Folder To Create' | |
#> | |
Function Create-SPOFolder(){ | |
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/folders" | |
$folderPayload = @{ | |
__metadata = @{'type' = 'SP.Folder' }; | |
ServerRelativeUrl = $FolderUrl; | |
} | ConvertTo-Json | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO –Url $Url –Method Post –UserName $UserName –Password $Password –Metadata $folderPayload –RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue | |
} | |
<# | |
.SYNOPSIS | |
Update Folder | |
.DESCRIPTION | |
Update Folder operation via SharePoint 2013 REST API. | |
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name') | |
method: POST | |
body: { '__metadata': { 'type': 'SP.Folder' }, 'Name': 'New name' } | |
Headers: | |
Authorization: "Bearer " + accessToken | |
X-RequestDigest: form digest value | |
"IF-MATCH": etag or "*" | |
"X-HTTP-Method":"MERGE", | |
accept: "application/json;odata=verbose" | |
content-type: "application/json;odata=verbose" | |
content-length:length of post body | |
.NOTES | |
Prerequisite : Invoke-RestSPO function | |
.EXAMPLE | |
Update-SPOFolder -WebUrl $WebUrl -UserName $UserName -Password $Password -FolderUrl '/Shared Documents/Folder To Update' -FolderName "New Folder Name" | |
#> | |
Function Update-SPOFolder(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$FolderUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$FolderName | |
) | |
$Url = $WebUrl + "/_api/web/GetFolderByServerRelativeUrl('" + $FolderUrl + "')" | |
$folderPayload = @{ | |
__metadata = @{'type' = 'SP.Folder' }; | |
} | |
if($FolderName) { | |
$folderPayload['Name'] = $FolderName | |
} | |
$folderPayload = $folderPayload | ConvertTo-Json | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO –Url $Url –Method Post –UserName $UserName –Password $Password –Metadata $folderPayload –RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue –ETag "*" –XHTTPMethod "MERGE" | |
} | |
<# | |
.SYNOPSIS | |
Delete Folder | |
.DESCRIPTION | |
Delete Folder operation via SharePoint 2013 REST API. | |
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder 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-SPOFolder -WebUrl $WebUrl -UserName $UserName -Password $Password -FolderUrl '/Shared Documents/Folder To Delete' | |
#> | |
Function Delete-SPOFolder(){ | |
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 + "')" | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO –Url $Url –Method Post –UserName $UserName –Password $Password –RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue –ETag "*" –XHTTPMethod "DELETE" | |
} |
Working with files
Folder resource: represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.
Endpoint URI: http://<site url>/_api/web/getfilebyserverrelativeurl(‘/<folder name>/<file name>‘)
Supported HTTP methods: GET | DELETE | POST (File resource)
The following examples demonstrates how to perform basic operations with File resource including:
- upload file into SharePoint
- download file from a SharePoint
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 | |
} |
Summary
To summarize, it was demonstrates how to perform basic operations with files and folders, in particular how to download and upload files via REST. For that purpose we utilized Invoke-RestSPO function that is intended for sending HTTPS requests to O365/SharePoint Online REST service.
Hi, thanks for the scripts.
I am trying to upload a file(s) to our sharepoint online site using the Upload-SPOFile function but get an error:
The process cannot access the file….because it is being used by another process.
I can’t figure out what that process would be as nothing is open locking that file that I am aware of…it’s just a word doc by itself in a folder.
Appreciate your help and time.
Hi, ignore my last comment…I have got the file unlocked that I was trying to upload.
Other question I have though is being able to check in the file…I have it uploading fine to SharePoint Online…but it leaves the file checked out….do you have code examples of how to check in the file using Rest in PowerShell?
I’m actually getting this error below…unauthorized….
Sorry…here is the error…The Remote Server returned an error (401) Unauthorized…
Am I missing a step that I need to use a token somewhere….don’t see that in the code…but see some of that in your other posts.
Great stuff – but how do I call Download File if I’ve got an item result?
e.g.
Web/Lists(guid’2ba92156-a661-4159-bd0f-21bb9d06bd1a’)/Items(127)
Greetings,
probably you are looking for the following request:
/_api/web/listsguid’2ba92156-a661-4159-bd0f-21bb9d06bd1a’)/items(127)/file/$value
which will return file content of associated list item.
Unfortunately my HTTP Header to download the binary is ignored… I think I’ll just try the file URL directly/traditional download instead.