I have a folder structure similar to this on our SharePoint Online site:
Documents/
Drawings/
For Review/
Released/
Archived/
Documentation/
For Review/
Released/
Archived/
Pictures/
For Review/
Released/
Archived/
What I'm trying to do is write a function that will recursively search under just one of the directories, say, "Drawings", and return all of the filenames and their paths relative to Documents, ideally without hardcoding any of the subdirectories (e.g., the names may change or more may be added).
It was pretty easy to do a recursive query in a CamlQuery to find all of the Files under Documents, and I could just parse their paths to get what I want, but there should be a similar way to specify a server relative path and then descend recursively starting from there, but I haven't been able to figure it out.
Here is what I wrote to retrieve a file stream from a specific folder:
public static Stream GetFileFromSpecificLocation(string directoryPath, string fileName)
{
using var cc = new AuthenticationManager().GetACSAppOnlyContext(siteUrl,
appId, clientSecret);
List documents = cc.Web.Lists.GetByTitle("Documents");
cc.Load(documents.RootFolder);
cc.ExecuteQuery();
var combinedPath = $"{documents.RootFolder.ServerRelativeUrl}/{directoryPath.UrlPathEncode()}";
Folder f = cc.Web.GetFolderByServerRelativeUrl(combinedPath);
cc.Load(f, xf => xf.Files);
cc.ExecuteQuery();
foreach (var file in f.Files)
{
cc.Load(file, f => f.Name);
cc.ExecuteQuery();
if (file.Name != fileName) continue;
ClientResult<Stream> streamResult = file.OpenBinaryStream();
cc.Load(file);
cc.ExecuteQuery();
return streamResult.Value;
}
return null;
}
This is in a .NET Core application, so I'm a little limited in a few libraries.