C# Console Application and Office 365

With some if the things I have been doing recently I needed to create a test console application and connect it to Office 365 for testing API calls and a few other things. As I started out I realized it would be useful to write out the steps I took to get it to work, even if I am only the one that looks at them again.

Firstly, I created a new project in Visual Studio.

Now we have a project, we can install the SharePoint Online NuGet Package to allow us to actually connect with the right references. This is done by launching the “NuGet” windows in Visual Studio and running:

Install-Package Microsoft.SharePointOnline.CSOM

To read more about these components, you can browse to: https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM. Once it has finished installing your references should now look like this:

Now let’s write some code. Add the following using statements.

Now I added a class for storing the values needed for the site, username and password.

Now we can add some context code that will authenticate us against to Office 365.

Next just some simple code to iterate through the lists in the site.

Finally, modify the “Main” class to call the Lists method.

This code when ran will simply authenticate and then write out the list name in the specified site.

As you can see that was pretty straight forward to connect and retrieve. If we wanted to upload a file to Office 365 then there are various methods of doing this, one of which is based on saving the straight binary file direct to Office 365. I have added the following methods. For more details on the method you can use the following link:


Next I have updated the configuration class to store two new values.

The core method of the console is then updated to call the file upload method.

Once ran the file gets uploaded into the Office 365 SharePoint site.

There are other ways of doing this, by using OAuth tokens instead that utilize the Client ID and Client Secret that we would create. We will look at that approach later on.

Liam Cleary

Liam began his career as a Trainer of all things computer-related. He quickly realized that programming, breaking, and hacking was a lot more fun. He spent the next few years working within core infrastructure and security services until he found SharePoint. He is the founder and owner of SharePlicity, a consulting company that focuses on all areas of Technology. His role within SharePlicity is to help organizations implement technology that will enhance internal and external collaboration, document and records management, automate business processes, and of course security controls and protection. He is also a Microsoft MVP focusing on Architecture but also crosses the boundary into Development. He is also a Microsoft Certified Trainer (MCT). His specialty over the past few years has been security in SharePoint and its surrounding platforms. He can often be found at user groups or conferences speaking, offering advice, spending time in the community, teaching his kids how to code, raspberry PI programming, hacking the planet or building Lego robots.

You may also like...