我需要處理存盤在我的 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 Documents/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
}
我知道這種方法很糟糕。
uj5u.com熱心網友回復:
您可以使用clientContext.Web.GetFolderByServerRelativeUrl直接獲取檔案夾。
請參見使用檔案夾和檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/366866.html
