简体   繁体   English

如何使用C#将文件上传到Sharepoint在线文档库

[英]How to upload files to sharepoint online document library using c#

I have a requirement to upload files in to sharepoint online document library. 我需要将文件上传到sharepoint在线文档库中。 I have around 150 folders and with files and subfolders under them. 我大约有150个文件夹,下面有文件和子文件夹。 I need to upload all these files with the folder structure to a document library. 我需要将所有具有文件夹结构的文件上传到文档库。 I did some research regarding this, but i am not able to find the correct solution for this.i am dot net developer and looking for a solution for this. 我对此进行了一些研究,但我无法为此找到正确的解决方案。我是.net开发人员,正在为此寻求解决方案。

Can somebody please help me on how to upload the folders and sub-folders to a sharepoint online document library using c#? 有人可以帮助我如何使用c#将文件夹和子文件夹上载到sharepoint在线文档库吗?

Your help is really appreciable as this critical work for us. 您的帮助真的很宝贵,因为这对我们来说是一项至关重要的工作。

Thanks, Venu 谢谢,Venu

Asked and answered: https://sharepoint.stackexchange.com/questions/171184/upload-a-folder-with-sub-folders-and-files-recursively-with-pure-csom 问与答: https : //sharepoint.stackexchange.com/questions/171184/upload-a-folder-with-sub-folders-and-files-recursively-with-pure-csom

Here's an example of one that was already written: 这是一个已经写好的例子:

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);
}
}

To use it 使用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用 c# 在线将文件上传到 SharePoint 中的自定义列表 - Is it possible to upload files to a custom list in SharePoint online using c# SharePoint 2013 - 文档库上载c# - SharePoint 2013 - Document Library upload c# 如何使用c#在Sharepoint库子文件夹中上传文件? - How to upload a file in a Sharepoint library subfolder using c#? 列出Sharepoint在线文档库中的所有文件 - List all files in a Sharepoint online Document Library 使用 SSIS (vb.net / C#.net) 将压缩文件上传到 SharePoint 文档库 Office 365 - Upload zipped files to SharePoint Document Library Office 365 using SSIS (vb.net / C#.net) 使用C#将SharePoint库XML文档转换为PDF文档 - Convert SharePoint Library XML document to PDF document using C# 如何使用CSOM将(多个)文件上传到SharePoint Online? - How to upload (multiple) files to SharePoint Online using CSOM? 如何在使用 C# 将文档作为项目上传到 SharePoint 文档库时为自定义列添加值? - How to add value to a custom column while uploading document into a SharePoint document library as an item using C#? 使用 AddUsingPath 在线上传大文件到 SharePoint - Upload large files to SharePoint Online using AddUsingPath 使用C#获取SharePoint文档库位置 - Getting SharePoint document library location using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM