Sharepoint Client Object Model (CSOM) Get Files from Document Library Folder
Hi all,
Just want to share a function that shows how to get the files from a specific folder using CSOM:
HTH,
Andreas
Just want to share a function that shows how to get the files from a specific folder using CSOM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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(); } } |
Andreas
Comments
Post a Comment