How to call Office365 web service in a Console application using WCF
- by ybbest
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( "[email protected]", "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