'File Not Found Error When Uploading Files to Sharepoint
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
Solution 1:[1]
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();
Solution 2:[2]
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();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | RaytheonXie-MSFT |
