In this article you will learn how to connect to SharePoint 2013 Online using CSOM with Console Application.
Create a Console Application using Visual Studio 2013:
- Open Visual Studio 2013.
- Click on File, New, then Project.
- Select “Console Application” from installed templates, enter a name for the console and then click on Ok button.
- Right click on References in the Solution Explorer and then click on Manage NuGet Packages.
- Search for Microsoft.SharePointOnline.CSOM and then click on Install.
- This package requires a click-to-accept license.
- Installing the package.
- Package is installed successfully as shown below.
- Program.cs
- 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 CSOMOffice365
- {
- class Program
- {
- static void Main(string[] args)
- {
- string userName = "[email protected]";
- Console.WriteLine("Enter your password.");
- SecureString password = GetPassword();
-
-
- using(var clientContext = new ClientContext("https://c986.sharepoint.com"))
- {
-
- clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
-
- Web web = clientContext.Web;
-
- clientContext.Load(web);
-
- clientContext.ExecuteQuery();
-
- Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);
- Console.ReadLine();
- }
- }
- private static SecureString GetPassword()
- {
- ConsoleKeyInfo info;
-
- 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;
- }
- }
- }
Test the console 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.
Summary:
Thus in this article you have seen how to connect to SharePoint 2013 Online using CSOM with a console application.