Connect To SharePoint Online In Office 365 Using Console Application

With SharePoint Online, we are not able to access the SharePoint Server anymore. Hence, if we have to do updates, we have to either write an app that can be deployed to SharePoint Online or we can make use of the Swiss army knife – SharePoint Management Shell to get things done. Yet another powerful and old School approach is the use of the Console Application, which can harness the power of .NET managed CSOM (Client Side Object Model) modules.

SharePoint Online

Open Visual Studio and create a console Application.

console application

In order to work with SharePoint Client Object Model, we have to add two references to the Console Application,

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

SharePoint Client Object Model

Internal Implementation walk through

Add the references to the class file,

  1. usingSystem.Security;  
  2. usingMicrosoft.SharePoint.Client;  
Once the ‘SharePoint.Client references are added, create client context for the SharePoint Online site. Use the ‘SharePointOnlineCredentials' class to authenticate the SharePoint Online site. ‘SharePointOnlineCredentials’ represents an object which provides credentials to access SharePoint Online resources.

Load the lists present within the SharePoint Online site and execute the batch by calling Context.ExecuteQuery command. This would fetch all the lists within the site, which can be looped, using a simple for loop, as shown below:
  1. using(var context = new ClientContext(webSPOUrl))  
  2. {  
  3.     context.Credentials = new SharePointOnlineCredentials(userName, password);  
  4.     Web web = context.Web;  
  5.     context.Load(web.Lists,  
  6.         lists => lists.Include(list => list.Title,  
  7.             list => list.Id));  
  8.     context.ExecuteQuery();  
  9.     foreach(List list in web.Lists)  
  10.     {  
  11.         Console.WriteLine("List title is: " + list.Title);  
  12.     }  
  13. }  
Fetching Password as Secure String from Console

In order to use the password in the below statement it should be retrieved as a secure string from the Console Application. Though we can fetch the password from the console as a string, SharePoint online credentials expects a secure string. A string would generate an error.
  1. context.Credentials = new SharePointOnlineCredentials(userName, password);  
Let’s see how we can get the secure string password from console.
  1. string password = "";  
  2. //Read from console  
  3. ConsoleKeyInfo info = Console.ReadKey(true);  
  4. while (info.Key != ConsoleKey.Enter)  
  5. {  
  6.     //Add the password to the string variable  
  7.     if (info.Key != ConsoleKey.Backspace)  
  8.     {  
  9.         Console.Write("*");  
  10.         password += info.KeyChar;  
  11.     }  
  12.     else if (info.Key == ConsoleKey.Backspace)  
  13.     {  
  14.         //If backspace is pressed remove the character from password string  
  15.         if (!string.IsNullOrEmpty(password))  
  16.         {  
  17.             password = password.Substring(0, password.Length - 1);  
  18.             intpos = Console.CursorLeft;  
  19.             Console.SetCursorPosition(pos - 1, Console.CursorTop);  
  20.             Console.Write(" ");  
  21.             Console.SetCursorPosition(pos - 1, Console.CursorTop);  
  22.         }  
  23.     }  
  24.     info = Console.ReadKey(true);  
  25. }  
  26. Console.WriteLine();  
  27. varsecurePassword = new SecureString();  
  28. //Convert string to secure string  
  29. foreach(char c in password)  
  30. securePassword.AppendChar(c);  
  31. securePassword.MakeReadOnly();  
  32. returnsecurePassword;  
The full working code is shown below:
  1. using System;  
  2. usingSystem.Security;  
  3. usingMicrosoft.SharePoint.Client;  
  4. usingSystem.Collections.Generic;  
  5. usingSystem.Linq;  
  6. usingSystem.Text;  
  7. usingSystem.Threading.Tasks;  
  8. namespaceConnectToSPO  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             Console.WriteLine("SharePoint Online site URL:");  
  15.             stringwebSPOUrl = Console.ReadLine();  
  16.             Console.WriteLine("User Name:");  
  17.             stringuserName = Console.ReadLine();  
  18.             Console.WriteLine("Password:");  
  19.             SecureString password = FetchPasswordFromConsole();  
  20.             try  
  21.             {  
  22.                 using(var context = new ClientContext(webSPOUrl))  
  23.                 {  
  24.                     context.Credentials = new SharePointOnlineCredentials(userName, password);  
  25.                     Web web = context.Web;  
  26.                     context.Load(web.Lists,  
  27.                         lists => lists.Include(list => list.Title,  
  28.                             list => list.Id));  
  29.                     context.ExecuteQuery();  
  30.                     Console.ForegroundColor = ConsoleColor.White;  
  31.                     foreach(List list in web.Lists)  
  32.                     {  
  33.                         Console.WriteLine("List title is: " + list.Title);  
  34.                     }  
  35.                     Console.WriteLine("");  
  36.                 }  
  37.             }  
  38.             catch (Exception ex)  
  39.             {  
  40.                 Console.WriteLine("Error is: " + ex.Message);  
  41.             }  
  42.         }  
  43.         private static SecureStringFetchPasswordFromConsole()  
  44.         {  
  45.             string password = "";  
  46.             ConsoleKeyInfo info = Console.ReadKey(true);  
  47.             while (info.Key != ConsoleKey.Enter)  
  48.             {  
  49.                 if (info.Key != ConsoleKey.Backspace)  
  50.                 {  
  51.                     Console.Write("*");  
  52.                     password += info.KeyChar;  
  53.                 }  
  54.                 else if (info.Key == ConsoleKey.Backspace)  
  55.                 {  
  56.                     if (!string.IsNullOrEmpty(password))  
  57.                     {  
  58.                         password = password.Substring(0, password.Length - 1);  
  59.                         intpos = Console.CursorLeft;  
  60.                         Console.SetCursorPosition(pos - 1, Console.CursorTop);  
  61.                         Console.Write(" ");  
  62.                         Console.SetCursorPosition(pos - 1, Console.CursorTop);  
  63.                     }  
  64.                 }  
  65.                 info = Console.ReadKey(true);  
  66.             }  
  67.             Console.WriteLine();  
  68.             varsecurePassword = new SecureString();  
  69.             //Convert string to secure string  
  70.             foreach(char c in password)  
  71.             securePassword.AppendChar(c);  
  72.             securePassword.MakeReadOnly();  
  73.             returnsecurePassword;  
  74.         }  
  75.     }  
  76. }  
Output



Thus, we have seen how to access SharePoint Online, using Console Application remotely. This Console Application can be used to run on a schedule, using Windows task scheduler and it can be made to perform updates/operations in SharePoint Online. This can be used as an alternative to the SharePoint Timer job, which is present in SharePoint on premise.