How to Connect to SharePoint Online/Office 365 using CSOM?

Fellow SharePointers,

Here is the sample script to connect to the SharePoint online using Console solution in Visual studio 2013 Environment.

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;

namespace OneClick
{
class Program
{
static void Main(string[] args)
{

using (ClientContext clientContext = new ClientContext(“https://my.sharepoint.com/sites/Dev-APAC”))
{
SecureString password = new SecureString();

foreach (char c in “mypwd”.ToCharArray()) password.AppendChar(c);

clientContext.Credentials = new SharePointOnlineCredentials(“usename@my.onmicrosoft.com”, password);

Web web = clientContext.Web;

clientContext.Load(web);

clientContext.ExecuteQuery();

Console.WriteLine(web.Title);

Console.ReadLine();
}

}
}

}

Hope it helps !! Happy SharePointing 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *