【问题标题】:Download files from Sharepoint 365 using SSIS使用 SSIS 从 Sharepoint 365 下载文件
【发布时间】:2018-09-08 10:24:42
【问题描述】:

我有一个名为“S & P”的文档库 (Sharepoint 365),其中包含许多文件夹和子文件夹。我的要求是使用 SSIS 包将这些文件夹/子文件夹中存在的所有文件下载到我的本地/服务器文件夹。这可以通过使用脚本任务(C# 或 VB 代码)或执行流程任务(Powershell 或 Batch 脚本)来完成,但我无法提取任何文件。到目前为止,我已尝试使用 powershell - 失败和 C# 代码 - 失败。所有使用的代码都是从互联网上复制的(不太了解 C# 和 Powershell)。

将接受将这些文件从共享点检索到我的本地文件夹的任何解决方案(不能使用第 3 方或未经授权的工具)

SharePoint URL 结构如下-https://xyzcompany.sharepoint.com/sites/dm/S%20%20P/Forms/AllItems.aspx

注意:所有文件夹和子文件夹都在 S & P 文档库中

请帮忙!

【问题讨论】:

  • 到目前为止您尝试过哪些代码?您是否阅读过 SharePoint API?

标签: powershell sharepoint ssis office365 script-task


【解决方案1】:

关于通过C#代码从SharePoint文档库下载所有文件,这里有一个demo供大家参考,我已经测试成功了。

    /// <summary>
    /// download all files from Document Library
    /// </summary>
    /// <param name="context"></param>
    /// <param name="docLibName">MyDocumentLibrary</param>
    /// <param name="path">C:\\folder\\</param>
    public static void DownloadAllFilesFromDocLib(ClientContext context, string docLibName, string path)
    {
        if (!path.EndsWith("\\"))
        {
            path = path + "\\";
        }
        Web web = context.Site.RootWeb;

        List doclib = web.Lists.GetByTitle(docLibName);

        context.Load(doclib);

        context.Load(web);
        context.ExecuteQuery();

        FileCollection filesInRootFolder = doclib.RootFolder.Files;
        FolderCollection subfolders = doclib.RootFolder.Folders;
        context.Load(filesInRootFolder);
        context.Load(subfolders);
        context.ExecuteQuery();

        //download files from root folders
        foreach (Microsoft.SharePoint.Client.File file in filesInRootFolder)
        {
            FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);               
            System.IO.Stream fileOutputStream = fileInfo.Stream;
            System.IO.Stream fileInputputStream = new FileStream(path + file.Name,FileMode.OpenOrCreate, FileAccess.ReadWrite);
            byte[] bufferByte = new byte[1024 * 100];

            int len = 0;
            while ((len = fileOutputStream.Read(bufferByte, 0, bufferByte.Length)) > 0)
            {
                fileInputputStream.Write(bufferByte, 0, len);
                fileInputputStream.Flush();
            }
            fileInputputStream.Close();
            fileOutputStream.Close();

        }

        //download files from sub folders
        foreach (Microsoft.SharePoint.Client.Folder folder in subfolders)
        {
            //Remove the default folder "Forms"
            if (folder.Name == "Forms")
            {
                continue;
            }
            //create folder in local disk
            Directory.CreateDirectory(path+folder.Name);
            context.Load(folder.Files);
            context.ExecuteQuery();

            foreach (Microsoft.SharePoint.Client.File file in folder.Files)
            {
                FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
                System.IO.Stream fileOutputStream = fileInfo.Stream;
                System.IO.Stream fileInputputStream = new FileStream(path+folder.Name + "\\" + file.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                byte[] bufferByte = new byte[1024 * 100];

                int len = 0;
                while ((len = fileOutputStream.Read(bufferByte, 0, bufferByte.Length)) > 0)
                {
                    fileInputputStream.Write(bufferByte, 0, len);
                    fileInputputStream.Flush();
                }
                fileInputputStream.Close();
                fileOutputStream.Close();                  
            }
        }
    }

我的测试结果截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2013-09-22
    相关资源
    最近更新 更多