Intermittent "The remote server returned an error: (401) Unauthorized." on clientContext.ExecuteQuery

gsaunders

Member
Joined
Mar 30, 2021
Messages
16
Programming Experience
5-10
Target .NET4.7.2, Windows Forms App template

Environment:
  • Windows 10
  • Visual Studio 2019 16.9.4
Code Setup:
  • We have added Microsoft.SharePoint.Online.CSOM (16.1.2111.12000) Nuget Package
  • We have added Microsoft .Identity.Client (4.29.0) Nuget Package
Questions:

We are basically looping through a database with pointers to local files that are then getting pushed up to the sharepoint online stored in Library\Folders\SubFolders\NewFileName.

We have a huge number to transfer into sharepoint and in general things are flowing.

I believe I have shaken out most of the quirks, BUT we keep intermittently getting "The remote server returned an error: (401) Unauthorized." on different clientContext.ExecuteQuery(); commands.

This error could pop up after transferring 100 files or several thousand. In the end it just keeps stopping the flow of the application and we don't know why we are seeing the error.

We are getting a new access token ever 30 minutes just in case it was related to the token failing, but we still intermittently get this.

One of the commands is:
C#:
Microsoft.SharePoint.Client.ListItem items = uploadFile.ListItemAllFields;
clientContext.Load(items);
clientContext.ExecuteQuery();

It fails on other simple things intermittently for what seems like no reason.

I have built in a retry just in case it was due to loss of connection or some sort of internet blip, but the fix is just restarting the application or starting code at beginning of the completely process.

Thanks in advance for feedback.
 
Solution
Ok... so in the end this issue was due to access token expiring. Thought I had it properly getting new token, but it wasn't.

I couldn't find out how to refresh token so I am simply re-initiating the complete connection to get token once the first one expires.
Ok... so in the end this issue was due to access token expiring. Thought I had it properly getting new token, but it wasn't.

I couldn't find out how to refresh token so I am simply re-initiating the complete connection to get token once the first one expires.
 
Solution

Latest posts

Back
Top Bottom