In this SharePoint CSOM tutorial, we will discuss how to connect with SharePoint Online site using the client object model (csom) in the Visual Studio console application.
Before we start, we need to create a console application in Visual Studio. Here I am using Visual Studio 2010 but you can do the same thing in higher version of Visual Studio like Visual Studio 2015/2017/2019.
Console application to connect to SharePoint online using CSOM
Below is the steps to create a console application using Visual Studio to connect to SharePoint Online using CSOM.
Step 1: Open your Visual Studio
Step 2: Create a new console application and enter your solution Name and save it in your any drive location.
Step 3: Before we start, we need to add the SharePoint client object model dll in our application. For adding the dll we can add the dll manually under references or we can download it from NuGet gallery.
Step 4: For download the dll from NuGet, we have to go the manage NuGet packages under Tool in Visual Studio.
Step 5: Download the references which are required to connect SharePoint online. Below is the screenshot of the reference file which is required to download (Microsoft.SharePointOnline.CSOM).
Step 6: Next click on Install button. Once it will done, we will get an screen to select the project.
Step 7: Once you click on OK button , again you will get an screen to click Accept or Decline. Please click on Accept button to go forward.
Step 8: Once you click on Accept button, Download will start and you will get your all references inside reference file in your application.
Step 9: Next open your Program.cs file and write the below code under Main function.
CSOM code SharePoint online console application
Below is the CSOM code to connect to SharePoint Online site from the console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
namespace GetConnectWithSPOnline
{
class Program
{
static void Main(string[] args)
{
string userName = "[email protected]";
Console.WriteLine("Enter Your Password Please ------- ");
SecureString password = GetPasswordOfYourSite();
// ClienContext - Get the context for the SharePoint Online Site
using (var clientContext = new
ClientContext("https://pikasha12.sharepoint.com/sites/DLH"))
{
// SharePoint Online Credentials
clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
// Get the SharePoint web
Web web = clientContext.Web;
// Load the Web properties
clientContext.Load(web);
// Execute the query to the server.
clientContext.ExecuteQuery();
// Web properties - Display the Title and URL for the web
Console.WriteLine("Title: " + web.Title );
Console.WriteLine("URL: " + web.Url);
Console.WriteLine("Template: " + web.WebTemplate);
Console.ReadLine();
}
}
private static SecureString GetPasswordOfYourSite()
{
ConsoleKeyInfo info;
//Get the user's password as a SecureString
SecureString securePassword = new SecureString();
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter)
{
securePassword.AppendChar(info.KeyChar);
}
}
while (info.Key != ConsoleKey.Enter);
return securePassword;
}
}
}
Note:
- You have to update your current User Name in place my user Name
- Do not pass your password in code
Step 10 : Test the application:
Hit F5 to run the application. Enter the password and then click on Enter button.
You are successfully connected to SharePoint Online using CSOM.
You may like following SharePoint tutorials:
- SharePoint create folder programmatically using CSOM
- How to activate SharePoint Publishing Feature Programmatically using CSOM and PowerShell
- How to get access token in SharePoint Online using CSOM and use in Postman or Google Rest client
- Get SharePoint List Name and GUID using PowerShell and CSOM
- Get all lists and document libraries programmatically using CSOM in SharePoint Online
- SharePoint CSOM vs JSOM vs SSOM vs REST
- How to delete all items from SharePoint list
Hope this SharePoint tutorial explains, how to create a console application to connect to SharePoint Online site using visual studio and using the client-side object model (csom) code.
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).
[…] inside a console application. We are trying to connect to a SharePoint online site from the console application. For this, we have stored the Email Address and Password inside the App. Config […]
[…] SharePoint Online console application with csom […]
[…] I have created a console application and written the below method which will take a single parameter as URL (need to provide the site […]
[…] I have created a console application and connect to a SharePoint Online site to do this […]
Thank you sir! You have put me on right track.
Hi Sir, Getting error msg while execute the same code
” Cannot contact web site ‘https://xxx.sharepoint.com/’ or the web site does not support SharePoint Online credentials. The response status code is ‘Unauthorized’.”
Note :- I have given my credentials in program