Windows Form Application reading data from SharePoint Onlinesite.
A lot has already been written for reading data from a SP on-premisessite collections. But it took me some time to actually understand how to readdata from a SharePoint Online site.
First I would like to list down the errors which you mayface if the same code written for the site using Windows authentication is usedfor reading data from #SPO Site.
1. Invalid username and password.
2. User not authorized.
3. Remote server access forbidden.
The following code lines(for loading site context) worksfine in case we have Windows authentication for the site:
ClientContext context =new ClientContext("SiteCollectionUrl");
Web web = context.Web;
In case of Claims based authentication (e.g. in SPO sites):
The user must be able to sign into the remote system interactively. First, the user signs intoSharePoint through the federated authentication provider, and SharePoint mustissue its authentication cookies. Second, the code must retrieve theauthentication cookies. Third, those cookies must be added to the ClientContext object.
The article on msdn talks about how the forms application interacts with SPO using claims.
Below are the steps which can be followed for reading data from SPO site:
After these steps you can use the following code to loadcontext of the SPO Site:
string targetSite ="SPOSite Url";
using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite))
if (ctx != null)
ctx.Load(ctx.Web); // Query for Web
ctx.ExecuteQuery(); // Execute