使用 SSIS (vb.net / C#.net 将压缩文件上载到 SharePoint 文档库 Office 365

本文关键字:net SharePoint 上载 文档 文件 Office 压缩 SSIS vb 使用 | 更新日期: 2023-09-27 17:57:09

我需要将压缩文件从本地文件夹上传到 SharePoint 文档库。我无法使用 WebClient PUT 方法将此上载到 Office 365 SharePoint 网站。请使用 C# 或 VB.net 共享代码,因为我需要在 SSIS 包中实现这一点。

使用 SSIS (vb.net / C#.net 将压缩文件上载到 SharePoint 文档库 Office 365

有几个选项可用,具体取决于您对 SharePoint 服务器的访问。 请看以下内容:

使用 SSIS 自定义任务将文件上载到 SharePoint

如何将 Excel 文件从 SSIS 上载到 SharePoint 门户

文件类型不是那么重要,但您可能还需要将签入/签出过程与 SharePoint 合并。 您需要收集此过程的其他详细信息,最好同时与 SharePoint 管理员联系以获取详细信息。

我们可以使用脚本任务上传。首先,我们需要在运行 SSIS 包的系统中安装(共享点客户端)。

请参阅VS2015上的以下脚本任务C#代码

#region Namespaces
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Net;
using Microsoft.SharePoint;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;
using System;
using System.Data;
#endregion
namespace ST_14e7d12d30c54d54a1eaa0bfa5961c5b
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        public static class Utils
        {
            public static SharePointOnlineCredentials GetO365Credentials(string userName, string passWord)
            {
                SecureString securePassWord = new SecureString();
                foreach (char c in passWord.ToCharArray()) securePassWord.AppendChar(c);
                SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(userName, securePassWord);
                return credentials;
            }
        }
        public void Main()
        {
            try
            {
                //Set the Sharepoint URL (Till the folder that the files needs to be uploaded)
                string siteUrl = "https://AAAAAAAAA.sharepoint.com/BBBBBBBB/CCCCCCCC";

               //FQDN source file details passing from the package 
                string filePath = @Dts.Variables["User::SharePoint_SrcFile"].Value.ToString();
                //Destination File Path (inclueds file name and full share point adddress)
                string DestinationfilePath = @Dts.Variables["User::Sharepoint_DestLocation"].Value.ToString();
                //loging the processing file
                bool fireAgain = true;
                Dts.Events.FireInformation(0,"","Source File Processing:" + filePath + " || Share Point Dest:" + DestinationfilePath,"", 0,ref fireAgain);
                //Setting the library name (can find from the share point site)
                string libraryName = "Documents";
                ClientContext ctx = new ClientContext(siteUrl);
                ctx.RequestTimeout = 1000000;
                //Retriveng user name and password from project parameeter (Password set as sensitive)
                string sharepointusername = @Dts.Variables["$Project::sSharePointUserName"].Value.ToString();
                string sharepointPassword = @Dts.Variables["$Project::sSharePointPassword"].GetSensitiveValue().ToString();

                //Authenticating (using the sharepoint client installed)
                ctx.Credentials = Utils.GetO365Credentials(sharepointusername, sharepointPassword);
                Web web = ctx.Web;
                ctx.Load(web, a => a.ServerRelativeUrl);
                List docs = web.Lists.GetByTitle(libraryName);
                ctx.ExecuteQuery();

                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    FileCreationInformation flciNewFile = new FileCreationInformation();
                    flciNewFile.ContentStream = fs;
                    flciNewFile.Url = Path.GetFileName(filePath);
                    flciNewFile.Overwrite = true;
                    //Upload URL
                    flciNewFile.Url = DestinationfilePath.ToString() ;

                    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);
                    //ctx.Load(uploadFile);
                    uploadFile.ListItemAllFields["Title"] = "sharepoint";
                    uploadFile.ListItemAllFields.Update();
                    ctx.ExecuteQuery();
            }


        }
            catch (Exception ex)
            {
                Dts.Events.FireError(0,"", ex.Message, "", 0);
            }

        }
        #region ScriptResults declaration
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion