0

I am trying to connect to sharepoint online site using console application in VS 2017 but everytime it returns 400 bad request.

Same code works as expected in VS 2013

   System.Security.SecureString securestring = new System.Security.SecureString();
        foreach (char ch in password)
            securestring.AppendChar(ch);
        SharePointOnlineCredentials creds = new SharePointOnlineCredentials(userName, securestring);

            using (var clientContext = new ClientContext(siteUrl))
            {
                clientContext.Credentials = creds;
                Web web = clientContext.Site.RootWeb;

                clientContext.Load(web);
                clientContext.ExecuteQuery();
            }

Please help.

0

Try to follow up the steps below.

1.Create a new Console App project(ConsoleApp1) in Visual Studio 2017.

2.Install Microsoft.SharePointOnline.CSOM library using NuGet below.

Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.8412.1200

3.Add the following code into Program.cs file.

using System;
using Microsoft.SharePoint.Client;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "https://tenant.sharepoint.com/sites/lz";
            var userName = "lz@tenant.onmicrosoft.com";
            var password = "xxx";


            System.Security.SecureString securestring = new System.Security.SecureString();
            foreach (char ch in password)
                securestring.AppendChar(ch);
            SharePointOnlineCredentials creds = new SharePointOnlineCredentials(userName, securestring);

            using (var clientContext = new ClientContext(siteUrl))
            {
                clientContext.Credentials = creds;
                Web web = clientContext.Site.RootWeb;

                clientContext.Load(web);
                clientContext.ExecuteQuery();
                Console.WriteLine(web.Title);
                Console.ReadKey();
            }
        }
    }
}

4.Modify the "siteUrl", "userName" and "password" in the code, and run it using F5.

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.