使用 pnp.core 和 pnp.framework 在 C# 中访问共享点文件夹和文件的正确方法是什么?
What's the right way to access sharepoint folders and files in C# using pnp.core and pnp.framework?
我需要在我的 c# 应用程序上处理一些存储在 SharePoint 文件夹中的文件,但我找不到正确的方法来执行此操作。我正在使用 Microsoft.sharepoint.client、pnp.core 和 pnp.framework 作为库,我的代码类似于:
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();
}
例如,假设我需要访问文件夹的 URL 是“https://teamsite.msc.com/sites/mysite/Shared%20Documents/General/TestFolder/ “
有没有办法直接访问该文件夹?因为我发现访问它的唯一方法是做这样的事情:
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
}
据我所知,这种方法很糟糕。
您可以使用clientContext.Web.GetFolderByServerRelativeUrl
直接获取文件夹。
我需要在我的 c# 应用程序上处理一些存储在 SharePoint 文件夹中的文件,但我找不到正确的方法来执行此操作。我正在使用 Microsoft.sharepoint.client、pnp.core 和 pnp.framework 作为库,我的代码类似于:
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();
}
例如,假设我需要访问文件夹的 URL 是“https://teamsite.msc.com/sites/mysite/Shared%20Documents/General/TestFolder/ “ 有没有办法直接访问该文件夹?因为我发现访问它的唯一方法是做这样的事情:
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
}
据我所知,这种方法很糟糕。
您可以使用clientContext.Web.GetFolderByServerRelativeUrl
直接获取文件夹。