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

2 Answers 2

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.

7
  • 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, 2019 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, 2019 at 4:37
  • Yes, I am able to add manually list items in SharePoint online list.
    – P.Parikh
    Sep 17, 2019 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, 2019 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, 2019 at 4:22
0

Used below code :

It’s worth checking if you have site collection admin rights on the site. It’s a common misconception that if you have Global Admin rights or a SharePoint Online Admin role, You’ll automatically get admin access to SharePoint! You must add your account as a site collection administrator to each site.

string siteUrl = ConfigurationManager.AppSettings["SiteUrl"];
string clientID = ConfigurationManager.AppSettings["ClientId"];
string clientSecret = ConfigurationManager.AppSettings["ClientSecret"];
using (ClientContext clientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientID, clientSecret))
{
    List oList = clientContext.Web.Lists.GetByTitle("sDetails");
    clientContext.Load(oList);
    clientContext.Load(clientContext.Web);
    clientContext.Load(clientContext.Site);
    //clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
    clientContext.ExecuteQuery();
}

<appSettings>
    <add key="ClientId" value="XXX-XXX-XXX-XXX-XXX" />
    <add key="ClientSecret" value="XXXXXXXXXXXXXX" />
    <add key="SiteUrl" value="https://XXXXXX.sharepoint.com/sites/XXXXXX/" />       
  </appSettings>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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