0

Good morning, I´m currently stuck with an issue, I´m unable to find a solution for:

I want to connect to a on premise sharepoint 2016 using c#.

The library I use: Microsoft.SharePointOnline.CSOM Version 16.1.21909.1200 (most recent version)

My code:

                using (ClientContext context = new ClientContext(sharepointUri))
            {

                context.Credentials = new NetworkCredential("firstname.lastname", "password");                                                           
                Web web = context.Web;
                context.Load(web);
                context.ExecuteQuery();

This is however not working and returns an System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.' at the context.ExecuteQuery(); line.

I´ve seen numerous posts stating, that there is something wrong with the credentials, however when I login to the sharepoint using my browser, everything works fine.

I changed the Credentials to context.Credentials = new NetworkCredential(firstname.lastname", "password","domain"); -> correct domain and its still not working.

SiteUrl is correct as well, when accessing it from the browser it works fine as well.

Any ideas what the problem could be?

Murf
  • 113
  • 1
  • 12
  • 1
    You are either missing a quotation mark before `firstname.lastname`, or should remove the one after, but that may just be a mistake in the above example and not in your codebase, since it sounds like it compiles. – thesystem Jan 31 '22 at 08:56
  • yes, it was only a typo in the example, the code compiles – Murf Jan 31 '22 at 08:59
  • You may need to configure credentials with SharePointCredentialsManager instead of NetworkCredential according to this post/answer: https://stackoverflow.com/questions/61152196/an-unhandled-exception-of-type-system-net-webexception-occured-in-microsoft-shar – thesystem Jan 31 '22 at 09:04
  • In my case the URL was wrong :) – alex Jan 13 '23 at 13:04

1 Answers1

1

For those that will encounter this issue, one of the ways to resolve this issue was using .NET Framework (Windows) 4.75 or 4.8 version.

For some reason, you cannot connect to SharePoint OnPrem in .NET Core, .NET 6 or .NET 7 which are cross Platform. You will continually receive either error 401 unauthorized or 400 bad request even when your set up is correct.

So to connect to SharePoint 16 Onprem, you have to use.NET Framework (Windows). Also, you cannot add a .NET Framework(windows) Library to a .NET core application. You are still going to get 401 unauthorized. You can only add .NET Framework(windows) class library to a .NET Framework(windows) windows app.

For Azure Functions, you will need to select .NET Framework Isolated v4 as your target framework when connecting to SharePoint 16. It does not work on any other selected .NET Framework in Azure Function.

Finally, your username should be only the part before the domain name. eg,

if my username is abc@contoso.com, then I will have my setup like this;

`string siteUrl ="http://abc.mysharepoint.local/sites/mysite";
  string userName = "abc";
  string pwd = "mypassword";
  string domain = "contoso";
`

Hope this helps someone that will run into this issue.

Ucheuzor
  • 31
  • 6