0

How can I search thought REST API all files in Document Library(DocLib), inside sub-folders included. Let's say I have:

  • Folder 1A
    • Folder 1.1
      • File A;
  • Folder 2
    • File AA;
  • File AAA;

And I search a so I get:

  • File A;
  • File AA;
  • File AAA

Only files everywhere. Thanks in advance.

PS: I'm pretty new at SharePoint and Rest Api so please explain don't just give me the code. Thanks

1
  • out of curiosity, is there a need to do this via REST API rather than using SP Online's robust OOTB search, library search, or a customized search? If not: in your library / settings / advanced settings, check your Search options: enable items to be found in Search; indexing. Globally, manage your SP Online Search with this info: support.office.com/en-us/article/…. Good luck!
    – KRobison
    Nov 28, 2017 at 18:49

2 Answers 2

0

If you need to find items by filename the best way is to use CSOM with a CAML query. The important thing is to set the scope to RecursiveAll. It will traverse all folders and all files. The filename column is called FileLeafRef so you need to change below query accordingly.

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle(listTitle);
var query = new SP.CamlQuery();
query.set_viewXml('<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name="Modified" Ascending="FALSE"/></OrderBy></Query><RowLimit>1</RowLimit></View>');
var items = list.getItems(query);
ctx.load(items);
ctx.executeQueryAsync(
  function() {
    if(items.get_count() > 0) { 
       var item = items.getItemAtIndex(0); 
       Success(item);
    }
    else
    {
        Success(null);
    }
  },
  Error
);
0

use below query in REST API:

This will return all files with the phrase ‘Preview’ in the FileRef field):

/_api/web/lists/getByTitle('ListName')/items?$select=FieldValuesAsText/FileLeafRef&$expand=FieldValuesAsText&$filter=substringof('Preview',FileLeafRef)

where FileLeafRef is internal name for Name field in Document Lib

Your Answer

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

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