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).
Environment
- SharePoint Online/SharePoint 2013
- HTML
- Jquery
- Rest
Logic
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.
Process
- 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.
Document library used for the code. - 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.
$.ajax({ url: cur_url+"/_api/web/getfolderbyserverrelativeurl('CustomDocs')?$expand=Files,Files/ListItemAllFields", type: "GET", headers: { "Accept": "application/json;odata=verbose" }, success: function(data) { console.log(data); var items= data.d.Files.results; $.each(items,function(index, item) { $('#CustomDoc').append("<a href="+items[index].ServerRelativeUrl+">"+items[index].ListItemAllFields.Position_x0020_Title+"</a>"+items[index].ListItemAllFields.Category+""+items[index].TimeLastModified+" "); }); } }); });
</pre> <table id="CustomDoc" class="mce-item-table"> <tbody> <tr> <th>Position Title</th> <th>Category</th> <th>Last Modified</th> </tr> </tbody> </table> <pre>
This is the javascript code that you will put in the script section of your file. We have a table created with some table headers that we will use to display the values retrieved.
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
items[index].ServerRelativeUrl
To get the value of custom column
items[index].ListItemAllFields.Category
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.
This is working for me. thanks
LikeLike
This article has been a fantastic find for me – no one else was demonstrating how to access the custom column values once they were retrieved. Thank you so much.
I have one question; Would it be possible to add a filter clause to only pull back results which match, in your example, a specific category?
LikeLike
Hey Mark, Thanks for liking the article, I did struggle multiple times with this topic. That was the reason I thought of sharing this. You can always add a filter in the query normally as in any other query though I have not tried this yet. You can also add the filter once you get the data in the JSON array.
LikeLike
Thanks for this article! One of the best explanations online.
Perhaps would you know how to filter on the custom fields when using getfolderbyserverrelativeurl? Using your example, how would the url be structured if I had to require only the files where Category=’A’?
LikeLike
You can just add “and Category eq A” to the query. Or look for filter rest api query for lists in google you can try them similar. Paste the url in a browser first and then put in the application.
LikeLike
Thank you very much for sharing this, This has been really helpful. This worked for custom columns. However, I’m stuck at reading values from multi select choice field. I do not see these columns listed in JSON response. Not sure what I’m missing here. Could you please help?
Thanks,
SV
LikeLike
Sorry for late response. First of all browse the rest api result in plain browser and find your column name. Look at the structure of data returned in the json response.
LikeLike