簡體   English   中英

將文件上傳到Sharepoint上庫中的特定文件夾

[英]Upload files to a specific folder in a Library on Sharepoint

如果可能的話,我想為我提供幫助。 我希望能夠將文件上傳到Sharepoint Online上的文件夾。 目前,我只能上傳到圖書館。 下面是我當前正在使用的代碼。

private void BtnUpload_Click(object sender, EventArgs e)
{
    siteURL += "/" + tbDept.Text;

    try
    {
        using (ClientContext ctx = new ClientContext(siteURL))
        {
            SecureString passWord = new SecureString();
            foreach (var c in logUser.getPassword())
            {
                passWord.AppendChar(c);
            }
            ctx.Credentials = new SharePointOnlineCredentials(logUser.getUsername(), passWord);
            Web web = ctx.Web;

            foreach (string fname in arrAllFiles)   //arrAllFiles --> contains multiple files to be uploaded
            {
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Overwrite = true;
                newFile.Content = System.IO.File.ReadAllBytes(fname);
                newFile.Url = System.IO.Path.GetFileName(fname);
                List docs = web.Lists.GetByTitle(tbLibrary.Text); //tbLibrary --> Library's Name
                Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
                ctx.ExecuteQuery();
            }
            MessageBox.Show("Upload Complete!");
            siteURL = "https://mycompany.sharepoint.com";
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
        siteURL = "https://mycompany.sharepoint.com";
    }
}

該代碼工作正常。 我只需要添加功能即可將文件上傳到庫中的特定文件夾。

先感謝您。

這是我前一段時間寫的一些示例代碼。 您可以根據需要進行調整。 SaveBinaryDirect方法采用上下文和包含文件夾的相對路徑。 那應該工作。

ClientContext ctx = new ClientContext("http://sp13");

Folder folder = ctx.Web.GetFolderByServerRelativeUrl("Shared Documents");
string file = String.Concat(Environment.CurrentDirectory, @"\Files\SharePointGuidance2010.pdf");

List docLib = ctx.Web.Lists.GetByTitle("Documents");
ctx.Load(docLib);
ctx.ExecuteQuery();

using (MemoryStream stream = new MemoryStream( System.IO.File.ReadAllBytes(file) ) )
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/shared documents/SubFolder/spg.pdf", stream, true);
}

或嘗試對代碼進行以下編輯:

Folder folder = web.GetFolderByServerRelativeUrl("Documents/Subfolder");
FileCreationInformation fci = new FileCreationInformation();
fci.Content = System.IO.File.ReadAllBytes(@”..\..\myfile.txt”);
fci.Url = “myfile.txt”;
fci.Overwrite = true;
File fileToUpload = folder.Files.Add(fci);
ctx.Load(fileToUpload);

ctx.ExecuteQuery();

更新:以下代碼有效,我自己進行了測試:

var filePath = @"C:\PATH_TO_FILE\test.docx";

using (var context = new ClientContext("YOUR_SITE_URL") { Credentials = credentials })
{
    var folder = context.Web.GetFolderByServerRelativeUrl("/sites/YOUR_SITE/LIBRARY_INTERNAL_NAME/FOLDER_NAME");

    context.Load(folder);
    context.ExecuteQuery();

    folder.Files.Add(new FileCreationInformation
    {
        Overwrite = true,
        Content = System.IO.File.ReadAllBytes(filePath),
        Url = Path.GetFileName(filePath)
    });

    context.ExecuteQuery();
}

暫無
暫無

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

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