Authentication with SharePoint Online and the Client Side Object Model

With the Office 365 Beta opening not long ago people are starting to test the waters with regards to custom development and seeing what they can do etc…   We put out the SharePoint Online developers guide here to try and assist with that: https://msdn.microsoft.com/en-us/library/hh147180.aspx

However, one of the topics not addressed directly in that paper is how you can use the Client Side Object Model (CSOM) to interact with SharePoint Online.  You might want to do this if you are writing an application that wants to store or talk with data stored in SharePoint Online for example.

The CSOM allows you to do this in an easy to use manner by providing a library you can use to make calls to SharePoint, however, by default the CSOM doesn’t handle SharePoint Online’s authentication system. 

To address this gap we have published a paper and accompanying code sample projects to assist with showing how to accomplish this.

WhitePaper: Remote Authentication in SharePoint Online Using Claims-Based Authentication

Code Sample: https://code.msdn.microsoft.com/Remote-Authentication-in-b7b6f43c

In short, the code sample does the following:

Pops a browser dialog to authenticate you against SharePoint Online

Captures the various authentication cookies required and attaches them to a CSOM context.

Uses that CSOM context to request the SharePoint Online site’s “Title” property and writes it out.

Note:  If you want to use SharePoint web services then you would need to do almost the exact same thing, except attach the auth cookies to the web request instead.  Below is some code snippets to help with this:

// get the cookie collection
CookieCollection authCookie = ClaimClientContext.GetAuthenticatedCookies(targetSite, 925, 525);

listWS.Lists list = new listWS.Lists();

// attach the cookies to the List WS request
list.CookieContainer = new CookieContainer();
list.CookieContainer.Add(authCookie);

<go about setting up the rest of your List WS query here>

// execute the query
System.Xml.XmlNode nodes = list.GetListItems(listName, viewName, query, viewFields, rowLimit, null, string.Empty);

We hope this helps!

Thanks,

-Chris.