SharePoint File CRUD in REST
Tutorial: CRUD on Files using REST
Many projects revolve around CRUD operations on different types of Files. Hence, a tutorial is here to focus on CRUD on Files in SharePoint using Rest API.Course Booking Details:
Dates | Details | Offer | Book |
---|---|---|---|
Jan 21, 2019 – | SharePoint Rest Fundamentals | Less Price for Collab365 Readers | Book |

REST File By Thangu-Tutorial
Rest Url Construction for Files
You need to know the Server Relative Path before you start. The Server Relative Path mostly refers to the Link after the sharepoint site like https://thangu.sharepoint.com.If your site is https://thangu.sharepoint.com/sites/dev, can you guess the Server Relative Path?Yes, the ServerRelativePath is /sites/dev
Such paths are very useful if you refer the same file path from different domains and helps avoid broken links during migration.
There are two Functions which can be used to get the File.
- GetFolderByServerRelativeUrl – Get the File using Files keyword in the later part of URL.
- GetFileByServerRelativeUrl – Get the File directly.
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
How to read Files?
Search has become an integral part of life. Reading Files help to Search for the File you need. Various ODATA queries are available to read Files.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:
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:
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:
How to update the Content?
Update is similar to Getting Contents of a File Request , with a minute difference in the Request Type. The Request Type is PUT.Url: /_api/web/GetFolderByServerRelativeUrl(‘/sites/dev/Shared Documents’)/Files(‘demo.txt’)/$value
Request Type: PUT
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
For tutorial on CRUD on 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.
Comments
Post a Comment