Skip to content

Requesting SPFile.Exists with other properties gives a FileNotFoundException #10258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 9 tasks
digiofficerobin opened this issue May 14, 2025 · 2 comments
Open
1 of 9 tasks
Assignees
Labels
area:csom/rest/api Category: SharePoint Client Side Object Model SDK / REST API type:bug-suspected Suspected bug (not working as designed/expected). See “type:bug-confirmed” for confirmed bugs.

Comments

@digiofficerobin
Copy link
Contributor

digiofficerobin commented May 14, 2025

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.
@digiofficerobin digiofficerobin added the type:bug-suspected Suspected bug (not working as designed/expected). See “type:bug-confirmed” for confirmed bugs. label May 14, 2025
@Amey-MSFT Amey-MSFT self-assigned this May 15, 2025
@Amey-MSFT
Copy link

Hello @digiofficerobin,
Thank you for bringing this issue to our attention. We will look into it and get back to you shortly.

@Amey-MSFT Amey-MSFT added the area:csom/rest/api Category: SharePoint Client Side Object Model SDK / REST API label May 15, 2025
@Amey-MSFT
Copy link

Hello @digiofficerobin,
We were able to reproduce the issue, and we are investigating it. We have logged this as a bug, and our engineering team will look into it.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:csom/rest/api Category: SharePoint Client Side Object Model SDK / REST API type:bug-suspected Suspected bug (not working as designed/expected). See “type:bug-confirmed” for confirmed bugs.
Projects
None yet
Development

No branches or pull requests

2 participants