Using the rest api to retrieve information about my documents. The documents use managed metadata which isn't expanded correctly when using the stand REST api. So I found this fix which uses REST with a CAML query. That has been working very well up to now. The CAML query I use is

        var caml = "<View Scope='RecursiveAll'>" +
                      "<Query><Where><And>" +
                          "<BeginsWith>" +
                            "<FieldRef Name='FileRef' />"+
                            "<Value Type='URL'>"+filterStr+"</Value>" +
                          "</Begins>" +
                          "<Neq>" +
                            "<FieldRef Name='_ModerationStatus' />" +
                            "<Value Type='Number'>"+this.APPROVAL_STATUS.rejected.val+"</Value>" +
                          "</Neq>" +
                      "</And></Where>" +
                          "<OrderBy>" +
                            "<FieldRef Name='FLSequence' Ascending='TRUE' />" +
                          "</OrderBy>" +
                      "</Query>" +

This retrieves all kinds of information about my document, ID, Title, custom columns, properly expanded metadata columns. But the one thing it doesn't return is the name of the file!

In my searching the main problem people seem to be working on is how to filter by file name in a CAML query which doesn't help me much. I did try adding ViewFields in there:

     <FieldRef Name='LinkFilename' />
     <FieldRef Name='FileLeafRef' />
     <FieldRef Name='Name' />

But none of those pulled back the file name AND it also stopped returning all the other good info.

In this situation how would I go about retrieving the name of a document? Is there some kind of wildcard select all fields for a CAML query? Am I just SOL?

  • I also found sharepointmalarkey.wordpress.com/2010/10/12/… and tried BaseName and NameOrTitle, neither or which returned the name of the document.
    – Rothrock
    Commented Apr 29, 2016 at 19:37
  • It seems I can use the FieldValuesAsText, FieldValuesAsHtml, or FieldValuesForEdit __deferred uri to make another REST call and request the file name. This seems to be a bit over the edge complicated for getting the file name.
    – Rothrock
    Commented Apr 29, 2016 at 22:13

2 Answers 2


You need to add ?$expand=file to the end or your Url. And don't specify the ViewFields in the query.

For Example

var url = ($"{SPConfiguration.BaseAbsoluteUrl}_api/web/lists/getbytitle('{listTitle}')/getitems?$expand=file");
  • Are you saying that I can add REST $expand and $select query strings along with the CAML query for the filtering? This was quite a long time ago, but I don't think it ever occurred to me to combine the two.
    – Rothrock
    Commented Oct 10, 2018 at 15:03
  • @Rothrock you can combine the two, there are few query string parameters that don't work, but the $expand does. (IRC the paging can't be in query string when posting CAML) Commented Nov 4, 2018 at 16:39

This one is an alternative that I prefer to CAMLing with REST, but requires you to have the item ID and list ID.

var query = webUrl + '_api/web/lists(guid'<your list ID>')/items(<item ID>)/File;

Then you get the document name directly into data.d.Name.

I am unsure why CAML wouldn't bring FileLeafRef.

  • I'm using CAML with REST. I can't just use REST because we are using managed metadata which isn't properly expanded in REST. I could try rewriting the whole thing as a straight up CSOM/CAML query but most of the other things on the site/page are already using REST. I'm also trying get information on about 20 to 30 things at a time, so I'd prefer not to have to get all the IDs and then make second requests for all the info.
    – Rothrock
    Commented May 4, 2016 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.