Thursday, May 15, 2014

Get all files with their metadata under a particular folder using _api/web SharePoint service

When we create SharePoint 2013 workflows or apps we may need to use SharePoint web services to access data. Let’s assume that we need to get a list of files and their metadata under a specific folder in SharePoint document library.

We can use methods provided by “_api/web/” to access such information. In this post I'll explain about two methods I use to access files and their metadata. A full list of “_api/web/” on this regard can be found on this article.

1. GetFolderByServerRelativeUrl method to get list of files under a folder
In this sample scenario I have few files in a folder named Folder1 which is created in a SharePoint document library.
image 
I’ll use following service to get information about each file under that folder
http://sp13:8080/sites/1234/_api/web/GetFolderByServerRelativeUrl('Shared Documents/Folder1')/Files
As a result I got following information (extract for one entry only)
image
2. GetFileByServerRelativeUrl to get more information on a particular file
GetFolderByServerRelativeUrl()/Files provides us with limited set of information on a particular file (e.g.: ContentTypeId is missing). If we need further information we can use “GetFileByServerRelativeUrl“ method

To get more information about File1.txt we can use the following service
http://sp13:8080/sites/1234/_api/Web/GetFileByServerRelativeUrl('/sites/1234/Shared Documents/Folder1/File1.txt')/ListItemAllFields 
As a result I got following information 
image

No comments: