How to create console application to work with SharePoint Online Office 365 sites?

sharepoint online visual studio 2015 csom

In this post, we will discuss how we can create a console application to work with SharePoint online Office 365 sites. 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.

Follow the below steps:

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

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

SharePoint Online Office 365 csom
SharePoint Online Office 365 csom

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:

SharePoint Online client object model
SharePoint Online client object model

In the Manage Packages for Solution, click on 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 client object model csom

SharePoint Online client object model csomSelect one dll and then select the project and click on Install like below:

client object model sharepoint online

client object model sharepoint onlineThen it will show the Preview dialog box like below. Click on OK like below:

SharePoint online client object model c#
SharePoint online client object model c#

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

sharepoint online visual studio 2015 csom

sharepoint online visual studio 2015 csomTo 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:

csom visual studio 2015

csom visual studio 2015Then 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;

}

}

}


}

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About 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

View all posts by Bijay Kumar →