Brandon Michael Hunter
Brandon Michael Hunter

Reputation: 1221

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

I'm getting a list view threshold issue when attempting to upload a document to a document library. Let me give you the back story.

We've been using the same code for a while, but as of October 2019 things stopped working.

Any ideas ? Provided below is the code that I'm using

using (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(); 
}

Upvotes: 1

Views: 1313

Answers (2)

Lee
Lee

Reputation: 5493

My sample test code(the lib has more than 10k files).

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();
} 

Or(the folder has more than 10k files)

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();
} 

Upvotes: 0

Brandon Michael Hunter
Brandon Michael Hunter

Reputation: 1221

I found out the problem. The issue was the call ctx.Load(docs.RootFolder.Folders); I had removed this piece of code, I was now able to upload documents.

Upvotes: 1

Related Questions