Connect To SharePoint 2013 Online Using CSOM With Console Application

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.

    new

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

    Console

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

    References

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

    install

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

    accept

  7. Installing the package.

    package

  8. Package is installed successfully as shown below.

    Package

  9. Program.cs
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Net;  
    5. using System.Security;  
    6. using System.Text;  
    7. using System.Threading.Tasks;  
    8. using Microsoft.SharePoint.Client;  
    9. namespace CSOMOffice365   
    10. {  
    11.     class Program   
    12.     {  
    13.         static void Main(string[] args)   
    14.       {  
    15.             string userName = "[email protected]";  
    16.             Console.WriteLine("Enter your password.");  
    17.             SecureString password = GetPassword();  
    18.             // ClienContext - Get the context for the SharePoint Online Site  
    19.             // SharePoint site URL - https://c986.sharepoint.com  
    20.             using(var clientContext = new ClientContext("https://c986.sharepoint.com"))   
    21.             {  
    22.                 // SharePoint Online Credentials  
    23.                 clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
    24.                 // Get the SharePoint web  
    25.                 Web web = clientContext.Web;  
    26.                 // Load the Web properties  
    27.                 clientContext.Load(web);  
    28.                 // Execute the query to the server.  
    29.                 clientContext.ExecuteQuery();  
    30.                 // Web properties - Display the Title and URL for the web  
    31.                 Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);  
    32.                 Console.ReadLine();  
    33.             }  
    34.         }  
    35.         private static SecureString GetPassword()  
    36.       {  
    37.             ConsoleKeyInfo info;  
    38.             //Get the user's password as a SecureString  
    39.             SecureString securePassword = new SecureString();  
    40.             do   
    41.             {  
    42.                 info = Console.ReadKey(true);  
    43.                 if (info.Key != ConsoleKey.Enter)   
    44.                 {  
    45.                     securePassword.AppendChar(info.KeyChar);  
    46.                 }  
    47.             }  
    48.             while (info.Key != ConsoleKey.Enter);  
    49.             return securePassword;  
    50.         }  
    51.     }  
    52. }  

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.

    output

Summary:

Thus in this article you have seen how to connect to SharePoint 2013 Online using CSOM with a console application.

Up Next
    Ebook Download
    View all
    Learn
    View all