4

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?

6

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.

| improve this answer | |
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.

| improve this answer | |
0

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

enter image description here

| improve this answer | |

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.