none
C# working example for login, reading and writing to Office 365 SharePoint Online

    Question

  • Hi,

    Does somebody have a working solutions in C# for login to Office 365 SharePoint Online?

    I was find a multiple posts with this and similar subject, but none of them are working.
    In one of samples I got an error "The Login server cannot issue the requested compact encrypted ticket because a Data Encryption Key (DEK) has not been uploaded to the site."

    In another one with claims auth. I got "The requested site does not appear to have claims enabled or the Login Url has not been set." because login sample can't get a login url with method 'OPTIONS' (The remote server returned an error: (405) Method Not Allowed.)

    Thanks in advance,
    Mladen
    Thursday, April 14, 2016 10:31 AM

Answers

  • You cannot use outlook.com account. You will need to use @yourtenant.onmicrosoft.comaccount or if you have verified domain for your tenant then use that account e.g. @contoso.com.


    Wednesday, April 20, 2016 9:18 AM
    Moderator

All replies

  • Check the following post. It shows you example on how to do common tasks using CSOM as well as CSOM+PowerShell. 

    https://www.itunity.com/article/completing-basic-operations-sharepoint-csom-api-powershell-1278

    Thursday, April 14, 2016 3:38 PM
    Moderator
  • Thanks AmitVasu.
    I was already read this post, this is basic use of CSOM copied from MS.

    There are examples as:

    https://code.msdn.microsoft.com/office365/Remote-Authentication-in-b7b6f43c
    http://www.wictorwilen.se/Post/How-to-do-active-authentication-to-Office-365-and-SharePoint-Online.aspx
    https://blogs.msdn.microsoft.com/cjohnson/2011/05/02/authentication-with-sharepoint-online-and-the-client-side-object-model/
    https://msdn.microsoft.com/en-us/library/hh147177.aspx
    http://www.vrdmn.com/2013/01/authenticating-net-client-object-model.html


    In this posts is explained a working solutions, but the are old. It is obviously that MS changed SharePoint Online version and authentication method.

    Thanks and regards,
    Mladen

    Thursday, April 14, 2016 5:52 PM
  • Can you please let us know what specific issue you are running into?
    Friday, April 15, 2016 9:39 AM
    Moderator
  • I have C# windows form application and where I need to add a reading and writing a table/list on company Office 365 SharePoint.

    I'm currently stuck at login. I have my user at outlook.com which is allowed to read and write that table/list. I was try multiple solutions that I find on WEB, in different .NET framework and SharePoint client components version.

    Maybe I going to wrong direction... Maybe exist another API solution for working with SharePoint online in C#.

    Regards,
    Mladen


    Friday, April 15, 2016 9:53 AM
  • When you say C# windows form application that does not help much. Are you using Client Side Object Model (CSOM) or are you using REST API to talk to SharePoint Online?


    Friday, April 15, 2016 11:56 AM
    Moderator
  • Sorry, the examples that I find use CSOM (Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.dll). And yes, everything that I was try is CSOM.

    Regards and thanks,
    Mladen
    Friday, April 15, 2016 7:20 PM
  • Use this example from Kirk's blog. This should get you started. Make sure you pass the User ID as shown in the example i.e. USER@YOURTENANT.onmicrosoft.com

    https://blogs.msdn.microsoft.com/kaevans/2014/02/23/call-o365-using-csom-with-a-console-application/

    Friday, April 15, 2016 8:25 PM
    Moderator
  • I was try now and I got a same error: "The Login server cannot issue the requested compact encrypted ticket because a Data Encryption Key (DEK) has not been uploaded to the site."

    Thanks and best regards,

    Mladen


    Mladen

    Monday, April 18, 2016 7:46 PM
  • Can you please post your code here?
    Tuesday, April 19, 2016 12:58 AM
    Moderator
  • My code is the same as in samples. Only difference is my user. I'm using my *@outlook.com account which is added (allowed) to access to one company SharePoint Online "https://company-my.sharepoint.com/personal/rscreator_company_com/_layouts/15/start.aspx#/Lists/Truck%20Management/AllItems.aspx"

    When I try from browser everything works fine, I can login and work on the allowed list. But from code I got an errors that I described in previous posts.

    string hostApp = "https://company-my.sharepoint.com/personal/rscreator_company_com/_layouts/15/start.aspx#/Lists/Truck Management/AllItems.aspx";
    msUser = "mladen******@outlook.com";
    msPass = "*******";
    
    using (var context = new ClientContext(hostApp))
    {
    	SecureString passWord = new SecureString();
    	foreach (char c in msPass.ToCharArray()) passWord.AppendChar(c);
    	context.Credentials = new SharePointOnlineCredentials(msUser, passWord);
    	context.Load(context.Web, w => w.Title);
    	context.ExecuteQuery();
    
    	txtResponseTitle.Text = context.Web.Title;
    }

    Regards and thanks,

    Mladen


    Mladen

    Wednesday, April 20, 2016 6:33 AM
  • When I handle SharePoint Online Credentials Executing WebRequest I got error "The Login server cannot issue the requested compact encrypted ticket because a Data Encryption Key (DEK) has not been uploaded to the site." on POST to login.live.com.

    company-my.sharepoint.com: https://company-my.sharepoint.com/ GET Content-Type: X-IDCRL_ACCEPTED: t
    msoid.outlook.com: http://msoid.outlook.com/FPUrl.xml GET 
    login.microsoftonline.com: https://login.microsoftonline.com/GetUserRealm.srf POST Content-Type: application/x-www-form-urlencoded
    login.live.com: https://login.live.com/rst2.srf POST Content-Type: application/soap+xml; charset=utf-8

    Regards and Thanks,


    Mladen

    Wednesday, April 20, 2016 8:24 AM
  • You cannot use outlook.com account. You will need to use @yourtenant.onmicrosoft.comaccount or if you have verified domain for your tenant then use that account e.g. @contoso.com.


    Wednesday, April 20, 2016 9:18 AM
    Moderator
  • This link is broken
    Thursday, November 16, 2017 5:13 PM