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.

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.