0

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
context.ExecuteQuery();
0

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'>
                                    <Query>
                                    </Query>
                                </View>";
            camlQuery.FolderServerRelativeUrl = "/DocLibrary/folder1";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            ListItem itemOfInterest = listItems[0];
            string creator = itemOfInterest.FieldValues["Created_x0020_By"].ToString();
            string title = itemOfInterest.FieldValues["Title"].ToString();
        }

UPDATED

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);
        clientContext.Load(docLib,
                           d => d.Title,
                           d => d.RootFolder.Name);
        clientContext.ExecuteQuery();

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

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

        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));
            clientContext.ExecuteQuery();
        }

        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. – Chris Harrington Jun 14 '16 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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