SharePoint / SharePoint/sp-dev-docs
CheckedOutByUser.UserPrincipalName raises ServerObjectNullReferenceException
Open
@Amey-MSFT is already working on this.
Since May 8, 2025.
area:csom/rest/api
type:bug-confirmed
- 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
Microsoft.SharePointOnline.CSOM 16.1.22810.12000
Describe the bug / error
We try to minimize the number of calls to SharePoint online due to throttling issues.
After a checkout we need to refresh the SPFile object to reflect the newest changes.
But after the refresh the SPFile.CheckedOutByUser.ServerObjectIsNull is still true, while the file is checked out.
Steps to reproduce
Code to reproduce issue.
// Create a context per thread as ClientContext is not thread safe
var clientContext = CreateClientContext(instance, client, sharePointAccessToken);
// Make sure this file exists and is not checked out already
var fileUrl = @"https://idbdevelopment.sharepoint.com/Shared%20Documents/document.docx";
var spfile = clientContext.Web.GetFileByUrl(fileUrl);
clientContext.Load(spfile, f => f.Exists, f => f.CheckOutType, f => f.CheckedOutByUser.UserPrincipalName);
clientContext.ExecuteQuery();
Console.WriteLine(spfile.CheckOutType.ToString()); // Should output 'None'
Console.WriteLine(spfile.CheckedOutByUser.ServerObjectIsNull); // Should output TRUE
spfile.CheckOut();
Console.WriteLine(spfile.CheckOutType.ToString()); // Should still output 'None' because the call is not executed yet
Console.WriteLine(spfile.CheckedOutByUser.ServerObjectIsNull); // Should still output TRUE because the call is not executed yet
clientContext.ExecuteQuery();
// Now load CheckOutType and CheckedOutByUser.UserPrincipalName again
clientContext.Load(spfile, f => f.Exists, f => f.CheckOutType, f => f.CheckedOutByUser.UserPrincipalName);
clientContext.ExecuteQuery();
Console.WriteLine(spfile.CheckOutType.ToString()); // None is changed to output 'Online'
// ISSUE:
// The ServerObjectIsNull is still true, while the JSON response from the SharePoint online API contains a checkout user object
Console.WriteLine(spfile.CheckedOutByUser.ServerObjectIsNull); // Should output FALSE
try
{
// Should output user principal name, but fails with exception:
// Microsoft.SharePoint.Client.ServerObjectNullReferenceException: 'Object reference not set to an instance of an object on server. The object is associated with identity
Console.WriteLine(spfile.CheckedOutByUser.UserPrincipalName);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
// Workaround is to create a new spfile object, but this is an extra unneeded call (increasing throttling issues)
spfile = clientContext.Web.GetFileByUrl(fileUrl);
clientContext.Load(spfile, f => f.Exists, f => f.CheckOutType, f => f.CheckedOutByUser.UserPrincipalName);
clientContext.ExecuteQuery();
}
Console.WriteLine(spfile.CheckedOutByUser.ServerObjectIsNull); // Should still output FALSE
Console.WriteLine(spfile.CheckedOutByUser.UserPrincipalName); // Should output user principal name
Expected behavior
After lines
// Now load CheckOutType and CheckedOutByUser.UserPrincipalName again
clientContext.Load(spfile, f => f.Exists, f => f.CheckOutType, f => f.CheckedOutByUser.UserPrincipalName);
clientContext.ExecuteQuery();
The SPFile.CheckedOutByUser.UserPrincipalName should contain the name of the user that has checked out the file.
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.