将文件上传到 SharePoint 时收到“远程服务器返回错误:(400) 错误请求”

问题描述 投票:0回答:2

我尝试使用 C# 脚本从 SSIS 任务将文件上传到 Sharepoint,但我不断收到此错误消息“远程服务器返回错误:(400) 错误请求”。我已经做了相当多的研究,但从我的代码中看不出我的做法与其他人尝试过的有何不同。这是我的代码:

        string filePath = @"J:\test\test.xlsx";
        string libraryName = "Shared Documents";
        string siteUrl = "https://tenant.sharepoint.com/sites/dev";
        string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
        SecureString passWord = new SecureString();
        foreach (char c in "Example0001#".ToCharArray()) passWord.AppendChar(c);
        string email = "[email protected]";

        try
        {
            using (ClientContext ctx = new ClientContext(siteUrl))
            {
                ctx.Credentials = new SharePointOnlineCredentials(email, passWord);
                FileCreationInformation fcInfo = new FileCreationInformation();
                fcInfo.Url = fileName;
                fcInfo.Overwrite = true;
                fcInfo.Content = System.IO.File.ReadAllBytes(filePath);

                Web myWeb = ctx.Web;
                List myLibrary = myWeb.Lists.GetByTitle(libraryName);
                myLibrary.RootFolder.Files.Add(fcInfo);
                ctx.ExecuteQuery();
            }
        } catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
c# sharepoint ssis
2个回答
0
投票

尝试相应地改变它

var file = myLibrary.RootFolder.Files.Add(fcInfo);
ctx.Load(file);
ctx.ExecuteQuery();

0
投票

我仍然有同样的错误。 @stuart你能解决吗?如果是的话,怎么办?

© www.soinside.com 2019 - 2024. All rights reserved.