I need to work with some files stored on the SharePoint folders on my c# application, but atm I can not find the right way to do that. I'm using Microsoft.sharepoint.client, pnp.core and pnp.framework as libraries and my code is something like:
string DocLibrary = "Documents";
try
{
using (var clientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientId, clientSecret))
{
Web web = clientContext.Web;
List DocumentLibrary = web.Lists.GetByTitle(DocLibrary);
clientContext.Load(web, w => w.Title);
clientContext.Load(DocumentLibrary);
clientContext.ExecuteQuery();
//part where I need to access the files
}
}
catch (Exception exp)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(exp.Message Environment.NewLine exp.StackTrace);
}
finally
{
Console.ReadLine();
}
Let's say for instance that I need to access that the URL of the folder is "https://teamsite.msc.com/sites/mysite/Shared Documents/General/TestFolder/" is there a way to access directly to the folder? Cause the only way I found to access this is by doing something like this:
clientContext.Load(DocumentLibrary.RootFolder);
clientContext.Load(DocumentLibrary.RootFolder.Folders);
clientContext.ExecuteQuery();
FolderCollection fcol = DocumentLibrary.RootFolder.Folders;
foreach (Folder f in fcol)
{
clientContext.Load(f);
clientContext.ExecuteQuery();
If(f.name="General")
//iteration on folder and files
}
And this approach as I know it's terrible.
CodePudding user response:
You can use clientContext.Web.GetFolderByServerRelativeUrl
to get the folder directly.