簡體   English   中英

將文件上傳到 Sharepoint 文件夾

[英]Upload file to Sharepoint folder

我已經能夠將文件上傳到 Documents,但是我不知道如何將文件上傳到“Doc​​uments.txt”中的文件夾之一。 我在網上查了一堆資源,似乎無法拼湊起來。 我正在嘗試使用 Microsoft.SharePoint.Client 在 C# WPF 桌面應用程序中完成這項工作。

用於將文件加載到文檔的當前工作代碼

    using (ClientContext context = new ClientContext(@"sharepointURL"))
        {
            // Credentials

            SecureString password = new SecureString();
            foreach (char c in "mypassword".ToCharArray()) password.AppendChar(c);
            context.Credentials = new 
            SharePointOnlineCredentials("myusername", password);

            var list = context.Web.Lists.GetByTitle("Documents");
            context.Load(list.RootFolder);
            var lib = list.RootFolder.Folders;
            context.ExecuteQuery();

            // Upload/replace file

            string fileName = @"myfilepath.docx";
            using (var stream = new FileStream(fileName, FileMode.Open))
            {
                string fileUrl = $@"{list.RootFolder.ServerRelativeUrl}/test.docx";
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fileUrl, stream, true);
                context.ExecuteQuery();
            }
        }

事實證明答案非常簡單。 我只是在 fileUrl 中包含文件夾名稱,如下所示:

string fileUrl = $@"{list.RootFolder.ServerRelativeUrl}/subfolder/test.docx";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM