5

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?

7

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.

  • 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. – Stefan Meyer Dec 23 '20 at 10:14
1

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.

0

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

enter image description here

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.