【问题标题】:How to upload files to sharepoint online document library using c#如何使用c#将文件上传到sharepoint在线文档库
【发布时间】:2018-01-10 17:14:05
【问题描述】:

我需要将文件上传到 sharepoint 在线文档库。我有大约 150 个文件夹,下面有文件和子文件夹。我需要将所有这些具有文件夹结构的文件上传到文档库。我对此进行了一些研究,但我无法找到正确的解决方案。我是 dot net 开发人员,正在寻找解决方案。

有人可以帮助我如何使用 c# 将文件夹和子文件夹上传到共享点在线文档库吗?

您的帮助对我们来说非常重要。

谢谢, 金星

【问题讨论】:

    标签: c# file-upload sharepoint-online sharepointdocumentlibrary


    【解决方案1】:

    提问和回答: https://sharepoint.stackexchange.com/questions/171184/upload-a-folder-with-sub-folders-and-files-recursively-with-pure-csom

    这是一个已经写好的例子:

    public class FileHelper
    {
        public static void UploadDocument(ClientContext clientContext, string sourceFilePath, string serverRelativeDestinationPath)
        {
        using (var fs = new FileStream(sourceFilePath, FileMode.Open))
        {
            var fi = new FileInfo(sourceFilePath);
            Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, serverRelativeDestinationPath , fs, true);
        }
    }
    
    public static void UploadFolder(ClientContext clientContext, System.IO.DirectoryInfo folderInfo, Folder folder)
    {
        System.IO.FileInfo[] files = null;
        System.IO.DirectoryInfo[] subDirs = null;
    
        try
        {
            files = folderInfo.GetFiles("*.*");
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine(e.Message);
        }
    
        catch (System.IO.DirectoryNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }
    
        if (files != null)
        {
            foreach (System.IO.FileInfo fi in files)
            {
                Console.WriteLine(fi.FullName);
                clientContext.Load(folder);
                clientContext.ExecuteQuery();
                UploadDocument(clientContext, fi.FullName, folder.ServerRelativeUrl + "/" + fi.Name);
            }
    
            subDirs = folderInfo.GetDirectories();
    
            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                Folder subFolder = folder.Folders.Add(dirInfo.Name);
                clientContext.ExecuteQuery();
                UploadFolder(clientContext, dirInfo, subFolder);
            }
        }
    }
    
    public static void UploadFoldersRecursively(ClientContext clientContext, string sourceFolder, string destinationLigraryTitle)
    {
        Web web = clientContext.Web;
        var query = clientContext.LoadQuery(web.Lists.Where(p => p.Title == destinationLigraryTitle));
        clientContext.ExecuteQuery();
        List documentsLibrary = query.FirstOrDefault();
        var folder = documentsLibrary.RootFolder;
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sourceFolder);
    
        clientContext.Load(documentsLibrary.RootFolder);
        clientContext.ExecuteQuery();
    
        folder = documentsLibrary.RootFolder.Folders.Add(di.Name);
        clientContext.ExecuteQuery();
    
        FileHelper.UploadFolder(clientContext, di, folder);
    }
    }
    

    使用它

    FileHelper.UploadFoldersRecursively(clientContext, @"C:\BigFolder", "Documents");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-12
      • 2011-08-25
      • 1970-01-01
      • 2010-10-02
      • 2016-12-06
      • 2013-10-08
      • 1970-01-01
      • 2010-10-10
      相关资源
      最近更新 更多