尝试使用CSOM和C#上载到Sharepoint在线图书馆时出现400错误的请求错误

时间:2020-10-29 16:58:45

标签: sharepoint-online csom

我正在尝试使用c#和CSOM将桌面上的文件上传到sharepoint在线网站集中的文档库中。下面是我的代码段。.有人可以让我知道我在这里缺少什么吗?

using (SP.ClientContext CContext = new SP.ClientContext(SiteUrl))
                {
                    CContext.Credentials = onlineCredentials;
                    SP.Web web = CContext.Web;
                    SP.FileCreationInformation newFile = new SP.FileCreationInformation();
                    byte[] FileContent = System.IO.File.ReadAllBytes(filePath);
                    //newFile.ContentStream = new MemoryStream(FileContent);
                    //newFile.ContentStream = new MemoryStream(FileContent);
                    newFile.Content = FileContent;
                    newFile.Url = "/sites/scr/Reports/" + Path.GetFileName(filePath);
                    newFile.Overwrite = true;

                    SP.List DocumentLibrary = web.Lists.GetByTitle(DocLibrary);                    
                    SP.File uploadFile = DocumentLibrary.RootFolder.Files.Add(newFile);

                    CContext.Load(DocumentLibrary);
                    CContext.Load(uploadFile);
                    CContext.ExecuteQuery();                                       
                }

1 个答案:

答案 0 :(得分:0)

尝试这样修改:

            using (ClientContext CContext = new ClientContext("https://tenant.sharepoint.com/sites/dev/"))
            {
                CContext.Credentials = new SharePointOnlineCredentials(account,secret);
                Web web = CContext.Web;
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(filePath));
                newFile.Url = "/sites/dev/Shared%20Documents/" + Path.GetFileName(filePath);
                newFile.Overwrite = true;

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

                CContext.Load(DocumentLibrary);
                CContext.Load(uploadFile);
                CContext.ExecuteQuery();
            }
相关问题