C# 从Sharepoint访问Excel文件的SSIS脚本任务

C# 从Sharepoint访问Excel文件的SSIS脚本任务,c#,excel,sharepoint,ssis,remote-access,C#,Excel,Sharepoint,Ssis,Remote Access,我有一段C#代码,可以让我在SharePoint中抓取一个Excel文件并放入本地文件夹 虽然我可以使用Windows登录访问该文件,但当我尝试运行该软件包时,会出现以下错误: 远程服务器返回错误:(403)禁止 这是我正在使用的一段代码: // downloads the file from SharePoint or a file system location to a local folder Dts.TaskResult = (int)ScriptRe

我有一段C#代码,可以让我在SharePoint中抓取一个Excel文件并放入本地文件夹

虽然我可以使用Windows登录访问该文件,但当我尝试运行该软件包时,会出现以下错误:

远程服务器返回错误:(403)禁止

这是我正在使用的一段代码:

        //  downloads the file from SharePoint or a file system location to a local folder
        Dts.TaskResult = (int)ScriptResults.Success;

        try
        {
            //  obtain location of local folder from variable
            DirectoryInfo dir = new DirectoryInfo(Dts.Variables["User::ImportFolder"].Value.ToString());
            if (dir.Exists)
            {
                //  Create the filename for local storage using
                //  the GUID from SharePoint as this will be unique.
                FileInfo file = new FileInfo((dir.FullName + ("\\"
                                + (Dts.Variables["User::WorkbookGUID"].Value.ToString() + Dts.Variables["User::Extension"].Value.ToString()))));
                if (!file.Exists)
                {
                    //  get the path of the file we need to download
                    string fileUrl = Dts.Variables["User::EncodedAbsUrl"].Value.ToString();
                    if ((fileUrl.Length != 0))
                    {
                        //  download the file from SharePoint or Archive file system to local folder
                        WebClient client = new WebClient();
                        if ((fileUrl.Substring(0, 4).ToLower() == "http"))
                        {
                            // download the file from SharePoint
                            client.Credentials = System.Net.CredentialCache.DefaultCredentials;
                            client.DownloadFile(fileUrl, file.FullName);
                        }
                        else
                        {
                            //  copy file from remote file system
                            System.IO.File.Copy(fileUrl, file.FullName);
                        }

                    }
                    else
                    {
                        throw new ApplicationException("EncodedAbsUrl variable does not contain a value!");
                    }

                }

            }
            else
            {
                throw new ApplicationException("ImportFolder does not exist!");
            }

        }
        catch (Exception ex)
        {
            Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }

        Dts.TaskResult = (int)ScriptResults.Success;

如果你按代码通过认证,它会起作用<代码>client.Credentials=新的SharePointOnlineCredentials(“yourlogin@yoursite.onmicrosoft.com“,密码)。此外,请检查文件路径是否正确生成。可能的重复项也请检查以下链接:我已从下面安装了SharePoint SDK,并尝试了“SharePointOnlineCredentials”,但仍会引发相同的错误。已尝试代码:SecureString SecurePassword=新SecureString();foreach(password.ToCharArray()中的字符c)SecurePassword.AppendChar(c);client.Credentials=新的Microsoft.SharePoint.client.SharePointOnlineCredentials(用户名、安全密码);client.DownloadFile(fileUrl,file.FullName);SDK链接:只是想分享我也发现了什么。您需要对Excel文件进行编码,获取该文件可能会很棘手。dll Microsoft.Sharepoint为您提供了SPList、SPListItem和SPFile等类,这些类可能是检索EncodedAbsUrl所必需的。缺点是dll只在服务器端,而不是开发人员端。您需要在计算机中安装Sharepoint Server才能获取服务器端dll。名为Microsoft.Sharepoint.client的客户端dll将为您提供SharePointOnlineCredentials,但这还不够。如果您通过代码传递凭据,它将起作用<代码>client.Credentials=新的SharePointOnlineCredentials(“yourlogin@yoursite.onmicrosoft.com“,密码)。此外,请检查文件路径是否正确生成。可能的重复项也请检查以下链接:我已从下面安装了SharePoint SDK,并尝试了“SharePointOnlineCredentials”,但仍会引发相同的错误。已尝试代码:SecureString SecurePassword=新SecureString();foreach(password.ToCharArray()中的字符c)SecurePassword.AppendChar(c);client.Credentials=新的Microsoft.SharePoint.client.SharePointOnlineCredentials(用户名、安全密码);client.DownloadFile(fileUrl,file.FullName);SDK链接:只是想分享我也发现了什么。您需要对Excel文件进行编码,获取该文件可能会很棘手。dll Microsoft.Sharepoint为您提供了SPList、SPListItem和SPFile等类,这些类可能是检索EncodedAbsUrl所必需的。缺点是dll只在服务器端,而不是开发人员端。您需要在计算机中安装Sharepoint Server才能获取服务器端dll。名为Microsoft.Sharepoint.client的客户端dll将为您提供SharePointOnline凭据,但这还不够。