SharePoint Online Runtime DLLs for C# Console

How to authenticate to SharePoint online using C# coding?

No comments

 25,496 total views,  2 views today

In this article, we will learn how to authenticate to SharePoint online using the C# coding through the .Net console application. Microsoft SharePoint Online is Software as a Service (SAAS) which part of Microsoft 365 (formerly known as Office 365). The various way we can connect or authenticate to SharePoint Online – CSOM (Client Side Object Model) is one of the approaches, in this article, we will learn how we can connect to SharePoint online remotely using the CSOM (Client Side Object Model) C# coding.


using Microsoft.SharePoint.Client;
using System;
using System.Linq;
using System.Security;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string siteCollectionUrl = "https://globalsharepoint2020.sharepoint.com";
string userName = "YourSPOUserName@yourtenant.onmicrosoft.com";
string password = "YourSPOPassword";
Program obj = new Program();
try
{
obj.ConnectToSharePointOnline(siteCollectionUrl, userName, password);
}
catch (Exception ex)
{
string msg = ex.Message.ToString();

}

}

public void ConnectToSharePointOnline(string siteCollUrl, string userName, string password)
{

//Namespace: It belongs to Microsoft.SharePoint.Client
ClientContext ctx = new ClientContext(siteCollUrl);

// Namespace: It belongs to System.Security
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);

// Namespace: It belongs to Microsoft.SharePoint.Client
ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);

// Namespace: It belongs to Microsoft.SharePoint.Client
Site mySite = ctx.Site;

ctx.Load(mySite);
ctx.ExecuteQuery();

Console.WriteLine(mySite.Url.ToString());
}
}
}

Note:

We must add the below two DLLs in the project assemblies by clicking on the project name from the right side solution explorer panel (right-click on the “Dependencies” then click on the “Add Reference”):

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime
SharePoint Online Runtime DLLs for C# Console
SharePoint Online Runtime DLLs for C# Console

Sometimes, we might get the “The remote server returned an error: (400) Bad Request.” error while running the above code – so, how to fix this error?

How to fix the remote server returned an error: (400) Bad Request error?

In order to fix the above, “the remote server returned an error: (400) Bad Request error”, we need to change the project target framework by going to the project tab, then clicking on the project property menu (here it is “ConsoleApp1 Properties”), then finally we need to click on the “Application” menu from the left side panel.

Then from the “Target framework” section, we have to change the framework, if you get the above error change the framework to 4.5 and build the solution again and debug the code again, now we should not face this error.

Change project target framework from C# console application
Change the project target framework from C# console application

Another way of authenticating to SharePoint Online: App Only Authentication

In-App Only Authentication we need to create the “client-id” and “client secret”, which can be configured in two steps:

  • Setup app-only principal
  • Grant permissions to the newly created principal

Setup app-only principal

Basically, this step is responsible to create the client id and client secret id.

  1. Open the appregnew.aspx page (https://sposite.sharepoint.com/_layouts/15/appregnew.aspx) here get the client id and client secret id.
  2. Assign permission to the app from the appinv.apsx page (https:// sposite.sharepoint.com/_layouts/15/appinv.aspx)

Note:

  • For the tenant level scoped permission – we need to open the URL like this – https://yourtenant-admin.sharepoint.com/_layouts/15/appinv.aspx)

Grant permissions to the newly created principal

In this step, we need to assign permission to the app, the permission could be any of the below:

  • Read
  • Write
  • Manage
  • FullControl

Along with the permission, we need to specify the scope of the permission, the scope could from any of the below:

Example:

<AppPermissionRequests> 
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/" Right="Write"/> 
</AppPermissionRequests>

Note:

For details on app-only authentication, please refer to this article – In 4 steps access SharePoint online data using postman tool

Summary:

Thus in this article, we have learned the below with respect to connecting to SharePoint Online using the CSOM (client-side object model) coding:

  • How to authenticate to SharePoint online using C# CSOM coding
  • How to connect to SharePoint Online using the C# CSOM (client-side object model) coding
  • How to fix the remote server returned an error: (400) Bad Request error?
  • How to access or authenticate SharePoint Online site data using the app only authentication model.