One struggle I always have working with Document Libraries is to retrieve the custom column values for each document. Let’s say we added a custom column called “Category” and now I want to read the value of that column for a document in the library.
In this article, we will use the help of Rest API and Jquery to retrieve the custom column value for a file in the document library and display using a content editor web part (you can use script editor web part).
- SharePoint Online/SharePoint 2013
We will use a rest API and jquery.
Rest API Web Service :currentsiteurl/_api/web/getfolderbyserverrelativeurl(‘documenturl’)?$expand=Files,Files/ListItemAllFields”
Breaking the Rest URL
Currentsiteurl= The URL of the site
getfolderbyserverrelativeurl(‘libraryurl)= Rest API that will get the files and its metadata values from the document library. It takes the document library URL as its input not to be confused with the title. You can read more about the Rest API here
$expand=Files,Files/ListItemAllFields =This is the property which tells the request what to serve. The Files will get you all the files in the folder or library with it’s out of the box metadata columns. When we add Files/ListItemAllFields then it will get you all the custom column in that particular library with their values. This is what will deliver us our custom column values.
So we will use this web service call with rest and get the result in JSON and we will retrieve the required value from the JSON.
- Create a document library in your SharePoint environment.
- add two custom columns
- Position Title
- Category(Choice Values= A,B,C,D)
- Upload a few files in the library as seen in the image below.
- Create an HTML file in a document library to input the code or you can directly put that code in a script editor web part too, which will also work fine.
- Write your code from the following section Code and JSON
in the file and save it.
- Copy the link of the HTML file and save it for later use.
- Now create a SharePoint web part page.
- Edit the page and add a content editor web part (you can add a script editor here and directly past all the code here too) and in the text link section, put the link to HTML file.
- Save your web part and page.
Code and JSON
Here we will try to understand the code and JSON structure and focus and what we need and how we are getting it.
var items= data.d.Files.results;
|Position Title||Category||Last Modified|
I will prefer to move step by step before copying the whole code. First try with the jquery only and lets investigate the JSON in the console to understand what it returns.
Here if you look at above image the rectangles drawn shows you the structure of the JSON.
In the above image, the rectangle shows you the out of the box columns that comes with the document library.
There are two nodes that say ListItemAllFields not to be mistaken with the one that holds the custom metadata columns added.
Once you expand the ListItemAllFields that will display all the custom columns that are added to this library.
IN the code this is what we are using
To get the value of OOB column
To get the value of custom column
So if you put all these code together in your html page or a script editor then this should be your result
I hope it helps you on your next journey with SharePoint. See you in the next article.