0

I am trying to insert items in SharePoint online list from c# console application using CSOM method, but every time it gives me only one error:

"The remote server returned an error: (401) Unauthorized." Exception thrown: 'System.Net.WebException' in Microsoft.SharePoint.Client.dll

My question would be how can I authenticate to SharePoint online list from my C# console application?

I have checked my credentials, everything is correct still I am getting this unauthorized error.

Here is my code in c#:

Insert item in SharePoint online list code

Please do reply if anyone has any idea how to resolve this error!

Thanks,
Shaily

0

Context you are creating is might create issue.

Whenever you are creating the context apply the credentials like below

context = new ClientContext(siteUrl);
context.Credentials = new SharePointOnlineCredentials(login, securePassword);
context.AuthenticationMode = ClientAuthenticationMode.Default;

Post that go for creation/accessing list object or site objects.

Also:

Since you are getting 401 Authorization error, request you to verify the account and credentials you are creating have sufficient permission on the site and list.

  • Thanks for the response Gaurravs, I have tried using your code but still getting that same error, I think I should have checked my account permissions, could you give me the direction where I can find that type of permission on SharePoint online account which allows me to authenticate successfully? – P.Parikh Sep 16 '19 at 18:10
  • open the siteurl in browser using same credentials, try to open and add a list item manually in the intended list. if it works. you have sufficient permissions then – Gaurravs Sep 17 '19 at 4:37
  • Yes, I am able to add manually list items in SharePoint online list. – P.Parikh Sep 17 '19 at 11:51
  • Can anyone please help me on this point? I have tried so many times its giving me just one error: 401 Unauthorized. – P.Parikh Sep 19 '19 at 16:38
  • can you post your updated code? Is there any 2 factor authentication in your organization, also, are you trying to run the console application inside your office network only ? – Gaurravs Sep 20 '19 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.