Working with folders and files via SharePoint 2013 REST in PowerShell

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.

Explore the REST service files and folder syntax

SharePoint 20123 Files and Folders REST syntax

 

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.

 

 

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

 

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.

References

Advertisements

7 thoughts on “Working with folders and files via SharePoint 2013 REST in PowerShell

  1. 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.

  2. 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?

  3. 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.

  4. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s