Call O365 using CSOM with a Console Application

This post shows how to use the SharePointOnlineCredentials class to authenticate to O365 from a console application.

Background

I write a ton of short samples for customers and co-workers.  I’ve written this one quite a few times but never seemed to add it to my personal source code control repository in the cloud (you are aware that you can get TFS in the cloud for free with Visual Studio Online, right?)  As I started adding this code to TFS today, I realized that I should also blog this one as it may help someone else.

In 2011, Wictor Wilen wrote a fantastic post that showed how to do active authentication to Office 365 and SharePoint Online.  While that post is still very accurate, that functionality has been brought into the client side object model so that you do not have to write this code yourself.  The CSOM for SharePoint 2013 introduces the new SharePointOnlineCredentials class that provides this functionality.

Show Me the Code!

To show you how easy this is, here is a Console application that uses the SharePointOnlineCredentials class to get a remote site’s Title property.

 using System;
using System.Security;
using Microsoft.SharePoint.Client;

namespace MSDN.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleColor defaultForeground = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Enter the URL of the SharePoint Online site:");

            Console.ForegroundColor = defaultForeground;
            string webUrl = Console.ReadLine();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Enter your user name (ex: kirke@mytenant.microsoftonline.com):");
            Console.ForegroundColor = defaultForeground;
            string userName = Console.ReadLine();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Enter your password.");
            Console.ForegroundColor = defaultForeground;
            SecureString password = GetPasswordFromConsoleInput();

            using (var context = new ClientContext(webUrl))
            {
                context.Credentials = new SharePointOnlineCredentials(userName,password);
                context.Load(context.Web, w => w.Title);
                context.ExecuteQuery();

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Your site title is: " + context.Web.Title);
                Console.ForegroundColor = defaultForeground;
            }
        }

        private static SecureString GetPasswordFromConsoleInput()
        {
            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;
        }
    }
}

Once you run the application, supply the URL, username, and password of a user that has permission to access the site using CSOM.  Here is what the output looks like:

image

For More Information

how to do active authentication to Office 365 and SharePoint Online

SharePointOnlineCredentials class

Connecting to Office 365 using Client Side Object Model and Web Services

Using PowerShell and the .NET CSOM to Query SharePoint 2013 Online