SharePoint / SharePoint/sp-dev-docs
Requesting SPFile.Exists with other properties gives a FileNotFoundException
@Amey-MSFT is already working on this.
Since May 15, 2025.
- Dominant language
- PowerShell
- Stars
- 1.4k
- Forks
- 1.1k
- Avg merge
- 4d 12h
- Merged PRs (30d)
- 12
Description
Target SharePoint environment
SharePoint Online
What SharePoint development model, framework, SDK or API is this about?
SharePoint CSOM
Developer environment
Windows
What browser(s) / client(s) have you tested
- 💥 Internet Explorer
- 💥 Microsoft Edge
- 💥 Google Chrome
- 💥 FireFox
- 💥 Safari
- mobile (iOS/iPadOS)
- mobile (Android)
- not applicable
- other (enter in the "Additional environment details" area below)
Additional environment details
.NET 6.0
Microsoft.SharePointOnline.CSOM 16.1.26017.12000
Describe the bug / error
This maybe by design, but it caused us a headache to figure this one out.
When we do a CSOM query to know if a file exists, then the the SPFile.Exists property is returned without exceptions. The SPFile.Exists contains a boolean with true or false.
When the Exists CSOM query is executed with more information such as list column names, then the SPFile.Exists contains also true or false without exceptions.
When more file fields are queried at once (to save some extra calls in order and avoid throttling), then an ServerException (FileNotFoundException) is thrown.
Steps to reproduce
Execute the following code fragment:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var siteUrl = @"https://....sharepoint.com/subsitent";
// Make sure this file does NOT exists
var fileUrl = $"{siteUrl}/dmsdocs/ThisFileShouldNotExists.txt";
var clientContext = new Microsoft.SharePoint.Client.ClientContext(new Uri(siteUrl));
// Hookup event handler to insert the access token
clientContext.ExecutingWebRequest += (sender, args) =>
{
args.WebRequestExecutor.RequestHeaders["Authorization"] = $"Bearer {sharePointAccessToken}";
};
var spfile = clientContext.Web.GetFileByUrl(fileUrl);
clientContext.ExecuteQuery();
clientContext.Load(spfile, f => f.Exists);
clientContext.ExecuteQuery();
// spfile.Exists == false
clientContext.Load(spfile, f => f.Exists, f => f.ListItemAllFields.ParentList.Fields.Include(fld => fld.InternalName, fld => fld.FieldTypeKind), f => f.ListItemAllFields);
clientContext.ExecuteQuery();
// spfile.Exists == false
clientContext.Load(spfile, f => f.Exists, f => f.TimeLastModified);
clientContext.ExecuteQuery(); // ISSUE: Throws ServerException (FileNotFoundExeption)
The last ExecuteQuery() call throws a ServerException.
This behavior is unexpected.
Expected behavior
We expected one way or the other, thus either:
- Always throw FileNotFoundException if file does not exists on server
- Always fill Exists with true or false. If file does not exists, then the other properties would raise a PropertyOrFieldNotInitializedException if we try to read these properties. If the file exists, the properties which are fetched should be accessible.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.