Create a console application to work with SharePoint Online using visual studio

This SharePoint CSOM tutorial, we will discuss, how to create a console application using visual studio to work with SharePoint Online Office 365.

In SharePoint Online we can not write server-side code, we can write only client-side code. We can create a console application to work with the client object model (csom/c#.net) code.

Create a console application to work with SharePoint Online using visual studio

Now, we will see a step by step tutorial, how to create a console application using a visual studio in SharePoint Online Office 365.

Follow the below steps:

Open visual studio 2015/2017/2019 and then File -> New -> Project…

Here from the left side, choose Templates -> Visual C#. And then from the list of template choose Console Application. Make sure to choose the .net framework version to “.NET Framework 4.5.2” like below:

sharepoint console application

Now our console application is ready, but to work with SharePoint online sites we need to add few dlls like below:

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll etc.

We can add those dlls like below. Click on Tools -> NuGet Package Manager -> Manage NuGet Packages for Solutions…. like below:

creating a simple console application in sharepoint 2013

In the Manage Packages for Solution, click on the Browse tab and search for “Microsoft.SharePoint.Client”. In the search result we can see like below dlls like below:

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
sharepoint online console

Select one dll and then select the project and click on Install like below:

sharepoint console application csom

Then it will show the Preview dialog box like below. Click on OK like below:

sharepoint 2013 csom console application

Once it will add the dll successfully, it will show a message like below:

csom sharepoint online console application

To connect to SharePoint online site, we need to use credentials which we can save in App.config like below:

<appSettings>
<add key=”SPOAccount” value=”********@OnlySharePoint2013.onmicrosoft.com” />
<add key=”SPOPassword” value=”********” />
</appSettings>

The app.config looks like below:

clientcontext credentials sharepoint online

Then in the Program.cs file we need to retrieve the user name and password from the App.Config file like below:

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings[“SPOPassword”])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings[“SPOAccount”];
}
catch
{
throw;
}
}

Then we can connect to the SharePoint online site using AuthenticationMode as ClientAuthenticationMode.Default;

And then we can pass the credentials to the context object. The full code looks like below:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace SPOnlineConsoleAppDemo
{
class Program
{
static void Main(string[] args)
{
using (ClientContext context = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/”))
{
// Use default authentication mode.
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
var web = context.Web;
context.Load(web, w => w.Webs);
context.ExecuteQuery();
}
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings[“SPOPassword”])
{
secureString.AppendChar(c);
}
return secureString;
}

catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings[“SPOAccount”];
}

catch
{
throw;
}
}
}
}

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we learned how to create a console application using visual studio 2017/2019/2013 to work with SharePoint Online Office 365.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

  • […] to create a console application to work with SharePoint Online Office 365 […]

  • >