Tutorial: How to create, read, update and delete files in SharePoint using REST

Thangu Thangu

Many projects revolve around CRUD (Create, Read, Update, Delete) operations on different types of Files. Hence, a tutorial is here to focus on CRUD on files in SharePoint using Rest API.

REST File By Thangu-Tutorial
REST File By Thangu-Tutorial

Rest Url Construction for Files

Before getting started, you need to know the “Server Relative Path”. The Server Relative Path mostly refers to the link after the SharePoint site. If your the full url to your site “https://thangu.sharepoint.com/sites/dev”, can you guess the Server Relative Path?

Yes, the ServerRelativePath is “/sites/dev”

Relative paths are very useful if you want to refer to a site when your code is running against different domains (e.g. test, staging) and helps to avoid broken links during migration.

There are two Functions which can be used to get the File.

  1. GetFolderByServerRelativeUrl – Get the File using Files keyword in the later part of URL.
  2. GetFileByServerRelativeUrl – Get the File directly.

Let’s assume we have a file called “NiceDay.txt” stored in a Shared Document Library?

To return the relative path we can use the function:

 GetFileByServerRelativeUrl(‘/sites/dev/Shared Documents/NiceDay.txt’)

Or

GetFolderByServerRelativeUrl(‘/sites/dev/Shared Documents’)/Files(‘NiceDay.txt’)

How to Create a New File?

A New File can be easily created with the following details:

  • URL- /_api/web/GetFolderByServerRelativeUrl(‘/sites/dev/Shared Documents’)/Files/add(url=’NiceDay.txt’,overwrite=true)
  • Data – Sunshine with Rainbow is nice
  • Request Type: POST

Here is a PowerShell script that Creates a File. You can download the RestCrud.ps1 from here before running the following:

$targetSite="https://xxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$serverRelativeUrl="/sites/dev"
$folderName="Shared Documents"
$fileName="NiceDay.txt"
$data="Wow!Today is a nice day!"
$restUrl="/_api/web/GetFolderByServerRelativeUrl('"+$serverRelativeUrl+"/"+$folderName+"')/Files/add(url='"+$fileName+"',overwrite=true)"
$newFile=Create-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data 
$newFile

How to read Files?

Search for content such as files has become an integral part of life. To assist this, there are various ODATA queries for us to read a file.

How to view all Files in a Library?

You get the Path to the File using the Server Relative Path and use the Files keyword at the end.

Url: /_api/web/GetFolderByServerRelativeUrl(‘/sites/dev/Shared Documents’)/Files

Request Type:GET

Below is a PowerShell script:

$targetSite="https://xxxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$folderName="Shared Documents"
$restUrl="/_api/web/GetFolderByServerRelativeUrl('/sites/dev/"+$folderName+"')/Files"
$files=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl 
$files.d.results|select Name,UIVersion

How to view a particular File in a Library?

You pass the parameter with the File Name after Files keyword. For example, if the File Name is demo.txt, then Files(‘demo.txt’). 

Url: /_api/web/GetFolderByServerRelativeUrl(‘/sites/dev/Shared Documents’)/Files(‘demo.txt’)

Request Type:GET

Below is a PowerShell script:

$targetSite="https://xxxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$folderName="Shared Documents"
$fileName="demo.txt"
$restUrl="/_api/web/GetFolderByServerRelativeUrl('/sites/dev"+$folderName+"')/Files('"+$fileName+"')"
$file=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl 
$file

How to view File Contents?

The query ends with $value.

Url: /_api/web/GetFolderByServerRelativeUrl(‘/sites/dev/Shared Documents’)/Files(‘demo.txt’)/$value

Request Type:GET

Below is a PowerShell script:

$targetSite="https://xxxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$folderName="Shared Documents"
$fileName="demo.txt"
$restUrl="/_api/web/GetFolderByServerRelativeUrl('/sites/dev"+$folderName+"')/Files('"+$fileName+"')/`$value"
$file=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl 
$file

How to update the Content?

Updating a file is very similar to getting the contents of a File, with a small difference in the Request Type. In this case the Request Type is PUT.

Url: /_api/web/GetFolderByServerRelativeUrl(‘/sites/dev/Shared Documents’)/Files(‘demo.txt’)/$value

Request Type: PUT

$targetSite="https://xxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$serverRelativeUrl="/sites/dev"
$folderName="Shared Documents"
$fileName="NiceDay.txt"
$data="Wow!Today is a nice day!-Updated"
$restUrl="/_api/web/GetFileByServerRelativeUrl('"+$serverRelativeUrl+"/"+$folderName+"/"+$fileName+"')/`$value"
$newFile=Update-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data -putonly $true

How to delete the Content?

The world is much better with recycling. What is better than Deleting? Recycling. REST has provided options to recycle data.

Url: /_api/web/GetFileByServerRelativeUrl(‘/sites/dev/Shared Documents/NiceDay.txt’)/recycle

Request Type: DELETE

$targetSite="https://xxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$restUrl="/_api/web/GetFileByServerRelativeUrl('/sites/dev/Shared Documents/NiceDay.txt')/recycle"
$filedeleted=Delete-SPObject -targetSite $targetSite -User $User -restUrl $restUrl

If you want to learn how to perform CRUD operations against List Items using REST, see  here.

If you are interested in my self-paced course to master REST Concepts in SharePoint for along with various App Development using REST, register here.

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 7$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos in the Collab365 Academy. This offer is insane and is only available for a limited period.