Search code examples
sharepointsharepoint-onlinecsom

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.

  • My site is on SPO and I'm using CSOM to create a folder and then add a uploaded document to that folder.

  • The service account I'm using to authentication to SPO is a site collection admin.

  • My CSOM code can create the folder, but cannot upload a document to that folder because SharePoint says that we've hit the list view threshold.

  • The document library that we're trying to upload a document to has over 20K items in it.

  • The default view that we're using only has one column selected, we have the Title and Created field indexed and the document library uses the modern experience.

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

Solution

  • 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.