Home > Office365, SharePoint Online, Web Services > How to call Office365 web service in a Console application using WCF

How to call Office365 web service in a Console application using WCF

In my previous post, I showed you how to call the SharePoint web service using a console application. In this post, I’d like to show you how to call the same web service in the cloud, aka Office365.In office365, it uses claims authentication as opposed to windows authentication for normal in-house SharePoint Deployment. For Details of the explanation you can see Wictor’s post on this here. The key to make it work is to understand when you authenticate from Office365, you get your authentication token. You then need to pass this token to your HTTP request as cookie to make the web service call. Here is the code sample to make it work.I have modified Wictor’s by removing the client object references.

static void Main(string[] args)
{
MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper( "admin@ybbest.onmicrosoft.com", "YourPassword","https://ybbest.sharepoint.com/");
HttpRequestMessageProperty p = new HttpRequestMessageProperty();
var cookie = claimsHelper.CookieContainer;

string cookieHeader = cookie.GetCookieHeader(new Uri("https://ybbest.sharepoint.com/"));
p.Headers.Add("Cookie", cookieHeader);
using (ListsSoapClient proxy = new ListsSoapClient())
{
proxy.Endpoint.Address = new EndpointAddress("https://ybbest.sharepoint.com/_vti_bin/Lists.asmx");

using (new OperationContextScope(proxy.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = p;
XElement spLists = proxy.GetListCollection();
foreach (var el in spLists.Descendants())
{
//System.Console.WriteLine(el.Name);
foreach (var attrib in el.Attributes())
{
if (attrib.Name.LocalName.ToLower() == "title")
{
System.Console.WriteLine("> " + attrib.Name + " = " + attrib.Value);
}
}
}
}
System.Console.ReadKey();
}
}

You can download the complete code from here.

Reference:

Managing shared cookies in WCF

How to do active authentication to Office 365 and SharePoint Online

Advertisements
  1. September 19, 2012 at 9:05 pm

    Hi, thanks for this post! Really helped me in getting to upload a document from C# to SharePoint online!

  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: