【问题标题】:How to upload file to a team site on Sharepoint online (Office 365)如何在线将文件上传到 Sharepoint 上的团队网站 (Office 365)
【发布时间】:2016-06-19 01:00:45
【问题描述】:

如何使用 C# 将文档添加到 SharePoint Online 中团队网站上的“文档”Web 部件?

我打算从 Intranet 运行我的代码。下面的代码看起来很有希望,但会引发 Microsoft.SharePoint.Client.IdcrlException。显然我的身份验证不正确,但我哪里出错了?

        using (ClientContext clientContext = new ClientContext("https://MyOrg.sharepoint.com/sites/MyTeamSite/"))
        {
            SecureString passWord = new SecureString();
            foreach (char c in "password".ToCharArray()) 
                passWord.AppendChar(c);
            clientContext.Credentials = new SharePointOnlineCredentials("myEmail@MyOrg.com", passWord);
            Web web = clientContext.Web;
            FileCreationInformation newFile = new FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes(@"c:\temp\test.txt");
            newFile.Url = "test.txt";

            List docs = web.Lists.GetByTitle("Documents");
            Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
            clientContext.ExecuteQuery();
        }

【问题讨论】:

标签: c# sharepoint-online


【解决方案1】:

您的代码似乎没问题。但是您使用的方法仅支持小于 2MB 的文件。这是由于信息从客户端发送到服务器的方式。该问题是由我们使用 FileCreationInformation 对象的 Content 属性引起的。如果您需要上传大于 2MB 的文件,请查看Large file upload with CSOM,它提供了 3 个选项来上传大文件。

【讨论】:

  • 我认为这不能回答问题。 OP 提到了身份验证问题,而您的解决方案涉及文件的大小。
猜你喜欢
  • 2019-03-10
  • 2013-10-08
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 2022-06-23
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多