Hopefully this is an easy question, but I am having some trouble finding samples of how to get a list of files in a sub folder in a document library.

Here's what I have, but I keep getting a "Value does not fall within the expected range" error when it does the ExecuteQuery().

// https://xxxxx.sharepoint.com/teams/site/DocLibrary/folder1
parentSiteURL = "https://xxxxx.sharepoint.com/teams/site";
libraryName = "DocLibrary";
folderName = "folder1";

ClientContext context = SetupSPContext(parentSiteURL, SharePointCRMDocumentsLoginName, SharePointCRMDocumentsPwd);

Web rootweb = context.Web;
string url2 = "/" + libraryName + "/" + folderName;
FolderCollection folderCollection = rootweb.GetFolderByServerRelativeUrl(url2).Folders;

// Don't just load the folder collection, but the property on each folder too
context.Load(folderCollection, fs => fs.Include(f => f.ListItemAllFields));

// Actually fetch the data

1 Answer 1


The key is to set the FolderServerRelativeUrl of the CamlQuery. Setting the Scope attribute to Recursive will return all items but not folders, setting it to RecursiveAll will bring back items plus folders.

Please try below code to get the documents from folder1 inside the DocLibrary

public static void GetListItemsInFolder()
            ClientContext clientContext = new ClientContext("https://xxxxx.sharepoint.com/teams/site");
            List list = clientContext.Web.Lists.GetByTitle("DocLibrary");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='Recursive'>
            camlQuery.FolderServerRelativeUrl = "/DocLibrary/folder1";
            ListItemCollection listItems = list.GetItems(camlQuery);
            ListItem itemOfInterest = listItems[0];
            string creator = itemOfInterest.FieldValues["Created_x0020_By"].ToString();
            string title = itemOfInterest.FieldValues["Title"].ToString();


You can use below function to get the files from a specific folder using CSOM

public List<file> ListFiles(string libraryName, string folderName)
    using (ClientContext clientContext = new ClientContext("your site url")
        List<portalfile> fileList = new List<portalfile>();

        var domain = "domain";
        var username = "username";
        var password = "password";
        clientContext.Credentials = new NetworkCredential(username, password, domain);

        Web web = clientContext.Web;
        ListCollection lists = web.Lists;

        var docLib = web.Lists.GetByTitle(libraryName);
                           d => d.Title,
                           d => d.RootFolder.Name);

        String folderUrl = String.Format("/{0}/{1}", docLib.RootFolder.Name, folderName);

        var folders = docLib.RootFolder.Folders;
        clientContext.Load(folders, fldrs => fldrs.Include(fldr => fldr.ServerRelativeUrl));

        var folder = folders.FirstOrDefault(f => f.ServerRelativeUrl.ToLower() == folderUrl.ToLower());
        if (folder != null)
            var files = folder.Files;
            clientContext.Load(files, fls => fls.Include(fl=> fl.Name));

        return files.ToList();
  • I couldn't get the 1st method to work, but I did get the 2nd one to work with one small change. I changed the FirstOrDefault query on the folders to var folder = folders.FirstOrDefault(f => f.ServerRelativeUrl.ToLower().EndsWith(folderUrl.ToLower())); The folderURL was shorter than the serverRelativeURL, but the EndsWith caught it. Jun 14, 2016 at 20:41

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.