Home > Back-end >  File Not Found Error When Uploading Files to Sharepoint
File Not Found Error When Uploading Files to Sharepoint

Time:02-25

I am getting the File Not Found exception in the ExecuteQuery() method but the file is sitting there in the mentioned folder.

I have included my code below. I am working on a .NET core application.

FileCreationInformation newFile = new FileCreationInformation();
newFile.Url = System.IO.Path.GetFileName(filePath);
newFile.Content = System.IO.File.ReadAllBytes(filePath);

Folder library = SPClientContext.Web.Folders.GetByUrl(fullFolderUrl);
Microsoft.SharePoint.Client.File uploadFile = library.Files.Add(newFile);
SPClientContext.Load(uploadFile);
SPClientContext.ExecuteQuery();



Microsoft.SharePoint.Client.ServerException
  HResult=0x80131500
  Message=File Not Found.
  Source=Microsoft.SharePoint.Client.Runtime
  StackTrace:
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at SharePointTest.Program.<Main>d__0.MoveNext() in C:\Users\O

CodePudding user response:

You need to at least call Load and ExecuteAsync method after you call GetByUrl method on library

FileCreationInformation newFile = new FileCreationInformation();
newFile.Url = System.IO.Path.GetFileName(filePath);
newFile.Content = System.IO.File.ReadAllBytes(filePath);

Folder library = SPClientContext.Web.Folders.GetByUrl(fullFolderUrl);
SPClientContext.Load(library);
SPClientContext.ExecuteQuery();
var files = library.Files;
SPClientContext.Load(files);
SPClientContext.ExecuteQuery();
Microsoft.SharePoint.Client.File uploadFile = files.Add(newFile);
SPClientContext.Load(uploadFile);
SPClientContext.ExecuteQuery();

CodePudding user response:

Please check whether the path of the file you need to upload is correct , then check the path of “fullFolderUrl” , you need to fill in the exact path of the specific library that you want to upload to.

Here is an example that will upload a file to SharePoint library:

static void Main(string[] args)
{
     string filePath = "C:\\Users/Administrator.SP01/Desktop/New folder/test.xlsx";
     string fullFolderUrl = "https://abc.sharepoint.com/sites/xxx/LibraryTest";
     var SPClientContext = GetonlineContext();
     Web web = SPClientContext.Web;
            
     FileCreationInformation newFile = new FileCreationInformation();
     newFile.Url = System.IO.Path.GetFileName(filePath);
     newFile.Content = System.IO.File.ReadAllBytes(filePath);

     Folder library = SPClientContext.Web.Folders.GetByUrl(fullFolderUrl);
     Microsoft.SharePoint.Client.File uploadFile = library.Files.Add(newFile);
     SPClientContext.Load(uploadFile);
     SPClientContext.ExecuteQuery();
}
  • Related