2013년 2월 17일 일요일

오피스365 webservice call in C#

오피스365 webservice call in C#



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

오피스365 webservice 를 호출하는 방법 입니다. 

  1. static void Main(string[] args)  
  2. {  
  3. MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper( "admin@ybbest.onmicrosoft.com""YourPassword","https://ybbest.sharepoint.com/");  
  4. HttpRequestMessageProperty p = new HttpRequestMessageProperty();  
  5. var cookie = claimsHelper.CookieContainer;  
  6.   
  7. string cookieHeader = cookie.GetCookieHeader(new Uri("https://ybbest.sharepoint.com/"));  
  8. p.Headers.Add("Cookie", cookieHeader);  
  9. using (ListsSoapClient proxy = new ListsSoapClient())  
  10. {  
  11. proxy.Endpoint.Address = new EndpointAddress("https://ybbest.sharepoint.com/_vti_bin/Lists.asmx");  
  12.   
  13. using (new OperationContextScope(proxy.InnerChannel))  
  14. {  
  15. OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = p;  
  16. XElement spLists = proxy.GetListCollection();  
  17. foreach (var el in spLists.Descendants())  
  18. {  
  19. //System.Console.WriteLine(el.Name);  
  20. foreach (var attrib in el.Attributes())  
  21. {  
  22. if (attrib.Name.LocalName.ToLower() == "title")  
  23. {  
  24. System.Console.WriteLine("> " + attrib.Name + " = " + attrib.Value);  
  25. }  
  26. }  
  27. }  
  28. }  
  29. System.Console.ReadKey();  
  30. }  
  31. }  

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.

You can download the complete code from here.
Reference:

댓글 없음: