7

I have this annoying issue when trying to connect to SharePoint site using Microsoft.SharePoint.Client:

var url = "https://company.sharepoint.com/sites/ADV-DEV/assigDev/bassie/SitePages/Home";
var clientContext = new ClientContext(url);
var oWebsite = clientContext.Web;
clientContext.Load(oWebsite);
clientContext.ExecuteQuery();

Console.WriteLine("Title: {0} Created: {1}", oWebsite.Title, oWebsite.Created);

On the line

clientContext.ExecuteQuery();

I just get

System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'

Even though that site definitely exists.

I tried removing each of the items from the url all the way down to just https://company.sharepoint.com but with the same results.

What is going on? Why is it so difficult just to connect to a SharePoint site?

1

5 Answers 5

8

The issue here was that VS created my console application as a .net core app, which as far as I'm aware does not work with the Microsoft.SharePoint.Client library.

I tried this previously in VSCode and it immediately threw an error saying the libraries are built for a different framework - I guess VS somehow doesn't know how to report that error.

After creating a new app using the standard framework, it seems to be working.

1
  • It is in the output window of VS: 1>c:\temp\vb.net-Sharepoint\Test 01\SP Test 01\SP Test 01\SP Test 01.vbproj : warning NU1701: Package 'Microsoft.SharePoint2016.CSOM 16.0.4690.1000' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net5.0-windows7.0'. This package may not be fully compatible with your project. Dec 23, 2020 at 10:14
2

Changing the Framework type did the trick. I am using VS 2019 (16.4.2) and my target framework was .net core 2.1. It was constantly giving 400 bad request errors. The same code when translated to Powershell was working perfectly fine.

When the target framework was changed to .net framework 4.7.2 (or 4.5), things worked perfectly. Try making a console application with framework as 4.7.2 and try things out.

1

use .net frame work 4.5 as target platform for sharepoint 2013

enter image description here

1

My entire project cluster is built on .Net Core 3.1.

I have been struggling for days to get the code, which is a simple connect and file upload, and only got the 400 Bad request error.

I created a POC console app and used the same SharePoint Libraries with .Net Standard 4.x and it works fine.

I fail to believe there isnt a .Net Core equivalent library that works.

Boggles the mind.

0

I had a new machine and I was getting this error on my VS2022. I was using the wrong project type. There are two Console App. see below screenshot. enter image description here

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.