Connect to Office 365 with CSOM

If you’re going to connect to SharePoint Online 2013 in Office 365, you’ll need to reference a few assemblies in your solution:

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime
  • System.Security

You’ll find these in the SharePoint Server 2013 Client Components SDK if you’ve not got them already.

In your code you’ll then need to create a ClientContext for your connection, including creating a SharePointOnlineCredentials object, and then you can start to talk to your SharePoint instance.

string url = "https://example.sharepoint.com/sites/testsite";
string username = "Office365User@example.com";
string userpass = "password";
using (ClientContext clientContext = new ClientContext(url))
{
	Console.WriteLine("Connecting to {0} as {1}", url, username);
	SecureString passWord = new SecureString();
	foreach (char c in userpass.ToCharArray()) passWord.AppendChar(c);
	clientContext.Credentials = new SharePointOnlineCredentials(username, passWord);
	Web web = clientContext.Web;
	clientContext.Load(web);
	clientContext.ExecuteQuery();
	Console.WriteLine("Got Web {0}", web.Url);
}

And this will be the basis of my future examples of doing things in Office 365 SharePoint using CSOM.

Advertisement
Connect to Office 365 with CSOM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.