SPO & CSOM & 无法将文档上传到文档库,但我可以创建文件夹

SPO & CSOM & Can't upload a document to a document library, but I can create a folder

我在尝试将文档上传到文档库时遇到列表视图阈值问题。让我告诉你背后的故事。

我们一直在使用相同的代码,但截至 2019 年 10 月,一切都停止了。

有什么想法吗?下面提供的是我正在使用的代码 使用 (ClientContext ctx = new ClientContext(SPUrl)) { ctx.Credentials = new SharePointOnlineCredentials(spuser, securePassword);

                            Web webSite = ctx.Web;
                            ctx.Load(webSite);
                            ctx.ExecuteQuery();
                            List list = ctx.Web.Lists.GetByTitle("TempDoc");
                            ctx.Load(list);
                            var lst = ctx.Web.Lists.GetByTitle("TempDoc");
                            var fld1 = lst.RootFolder.Folders.Add(FolderName);
                            fld1.Update();
                            ctx.ExecuteQuery();
                            foldersatus = true;



                            try
                            {
                                int FileLen;
                                FileLen = Request.Files[upload].ContentLength;
                                byte[] input = new byte[FileLen];
                                System.IO.Stream fileStream;
                                // Initialize the stream.
                                fileStream = Request.Files[upload].InputStream;
                                // Read the file into the byte array.
                                fileStream.Read(input, 0, FileLen);


                                FileCreationInformation newFile = new FileCreationInformation();

                                newFile.Content = input;
                                newFile.Url = filename;
                                List docs = webSite.Lists.GetByTitle("TempDoc");
                                ctx.Load(docs.RootFolder);
                                ctx.Load(docs.RootFolder.Folders);
                                Microsoft.SharePoint.Client.File uploadFile1 = docs.RootFolder.Folders[0].Files.Add(newFile);
                                ctx.ExecuteQuery(); }

我的示例测试代码(该库有超过 10k 个文件)。

List docs = context.Web.Lists.GetByTitle("largeLib1");                    
                var filePath = @"C:\Lee\template.xlsx";
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {                        
                    FileCreationInformation flciNewFile = new FileCreationInformation();

                    flciNewFile.ContentStream = fs;
                    flciNewFile.Url = System.IO.Path.GetFileName(filePath);
                    flciNewFile.Overwrite = true;                    
                    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);
                    context.Load(uploadFile);
                    context.ExecuteQuery();
                } 

或者(文件夹中文件超过10k)

Folder folder = context.Web.GetFolderByServerRelativeUrl("/sites/xxx/largeLib1/set2");
                var filePath = @"C:\Lee\template.xlsx";
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {                        
                    FileCreationInformation flciNewFile = new FileCreationInformation();

                    flciNewFile.ContentStream = fs;
                    flciNewFile.Url = System.IO.Path.GetFileName(filePath);
                    flciNewFile.Overwrite = true;                    
                    Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(flciNewFile);
                    context.Load(uploadFile);
                    context.ExecuteQuery();
                } 

我发现了问题。问题是调用 ctx.Load(docs.RootFolder.Folders);我删除了这段代码,现在可以上传文件了。