Unable to Retrieve SharePoint List Items using SharePointOnlineCredentials

ArunKumar Arumugam 60 Reputation points
2024-10-16T07:01:04.0033333+00:00

Hi Team,

I used the code below (with SharePointOnlineCredentials logic) to retrieve SharePoint list details. However, recently I encountered the exception mentioned below during execution, and I am unable to retrieve the list details. Could you please review the issue and provide suggestions to resolve it?

My code:

string sharePointSite = "Sharepoint site url";
var ctx = new ClientContext(sharePointSite);
string username = "******@g.com";
string password = "password";
ctx.Credentials = new SharePointOnlineCredentials(username, ConvertToSecureString(password));
Microsoft.SharePoint.Client.List myList = ctx.Web.Lists.GetByTitle("List Name");
ctx.Load(myList);
ctx.ExecuteQueryRetry();

Exception:

System.NotSupportedException: 'Cannot contact web site 'https://tenent.sharepoint.com/' or the web site does not support SharePoint Online credentials. The response status code is 'Unauthorized'. The response headers are 'X-NetworkStatistics=0,1051136,0,584,3894865,0,893428,189, X-SharePointHealthScore=0, X-MSDAVEXT_Error=917656; Access+denied.+Before+opening+files+in+this+location%2c+you+must+first+browse+to+the+web+site+and+select+the+option+to+login+automatically., SPRequestGuid=1e645aa1-60f8-6000-b48f-a, request-id=1e645aa1-60f8-6000-b48f-a, MS-CV=oVpkHvhgAGC0j6.0, Strict-Transport-Security=max-age=31536000, SPRequestDuration=19, SPIisLatency=0, MicrosoftSharePointTeamServices=16.0.0, X-Content-Type-Options=nosniff, X-MS-InvokeApp=1; RequireReadOnly, X-Cache=CONFIG_NOCACHE, X-MSEdge-Ref=Ref A: F2479F32C58442F6BC758C Ref B: MAA20106051 Ref C: 2024-10-16T06:47:43Z, Content-Length=0, Content-Type=text/plain; charset=utf-8, Date=Wed, 16 Oct 2024 06:47:42 GMT, P3P=CP="ALL DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI", X-Powered-By=ASP.NET'.'

Microsoft 365 and Office SharePoint Development
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2024-10-17T09:08:42.8466667+00:00

    Hi @ArunKumar Arumugam ,

    Welcome to Q&A forum!

    The exception you're encountering, System.NotSupportedException: 'Cannot contact web site 'https://tenent.sharepoint.com/' or the web site does not support SharePoint Online credentials., suggests that there may be an issue with the authentication method or permissions associated with the SharePoint site.

    Here are some suggestions to resolve the issue:

    1. Review the SharePoint Site URL: Double-check the SharePoint site URL in your code to ensure it is correct and accessible.
    2. Check Permissions: Ensure that the user account you are using has the necessary permissions to access the SharePoint site and the specific list.
    3. Verify Credentials: Double-check that you are using the correct credentials for SharePoint Online. Make sure your username and password are correct and that your account has the appropriate permissions.
    4. Automatic Login Option: If the error message mentions needing to browse to the site first, try accessing the SharePoint site in a browser and logging in manually to set up the session. Good day!

    If the answer is helpful, please click "Accept as Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.