使用SSIS从Sharepoint 365下载文件

时间:2018-03-29 12:45:15

标签: powershell sharepoint ssis office365 script-task

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

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

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

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

请帮忙!

1 个答案:

答案 0 :(得分:0)

关于通过C#代码从SharePoint文档库下载所有文件,这是一个供您参考的演示,我已经成功测试过。

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

我的测试结果的屏幕截图:

enter image description here

enter image description here