Connect To SharePoint 2013 Online Using CSOM With Console Application

Updated: Mar 18, 2019

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:

1. Open Visual Studio 2013.

2. Click on File, New, then Project.


3. Select “Console Application” from installed templates, enter a name for the console and then click on Ok button.











4. Right click on References in the Solution Explorer and then click on Manage NuGet Packages.











5. Search for Microsoft.SharePointOnline.CSOM and then click on Install.











6. This package requires a click-to-accept license.

















7. Installing the package.













8. 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 = "vijaianand@c986.onmicrosoft.com";              

Console.WriteLine("Enter your password.");              

SecureString password = GetPassword();   // ClientContext - Get the context for the SharePoint Online Site // SharePoint site URL - https://c986.sharepoint.com using(var clientContext = new ClientContext("https://c986.sharepoint.com"))               

{  

// 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 + "; URL: " + web.Url);                  

Console.ReadLine();              

}          }  

private static SecureString GetPassword()        

{              

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;          

}      }  }  


Test the console application:

  1. Hit F5 to run the application.

  2. Enter the password and then click on Enter button.

  3. 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.

Recent Posts

See All