title: How to connect to O365 MySite/OneDrive with CSOM?
body: <p>Is there a C# sample available that shows how we can connect to an O365 site programmatically and find all the MySite/OneDrive collections and appropriate authentication tokens, then connect to each of those via CSOM? Essentially we want to be able to download all content from all site collections in a particular tenant - we have it working for everything except MySites, but when I try to connect via CSOM to each user's mysite URL using the global administrator account's credentials I get Access Denied. It's certainly not possible/realistic for us to store each user's username and password in order to achieve this however.</p>
  
  <p>Edit:</p>
  
  <p>It turns out that it works fine via CSOM using the Global Admin credentials, as long as you don’t try to enumerate the sub-sites!
  However you <em>can</em> access the sub-sites if you know their URLs:</p>
  
  <pre><code>ClientContext cc = new ClientContext("http://contoso-my.sharepoint.com/personal/j_citizen_contoso_onmicrosoft_com/" );
  cc.Credentials = new SharePointOnlineCredentials("admin@contoso.onmicrosoft.com", AdminPassword);
  Web web = cc.Web;
  cc.Load(web, website =&gt; website.Webs);
  cc.ExecuteQuery();                     // - ServerUnauthorizedAccessException
  </code></pre>
  
  <p>But</p>
  
  <pre><code>ClientContext cc = new ClientContext("http://contoso-my.sharepoint.com/personal/j_citizen_contoso_onmicrosoft_com/subsite" );
  
  cc.Credentials = new SharePointOnlineCredentials("admin@contoso.onmicrosoft.com", AdminPassword);
  Web web = cc.Web;
  cc.Load(web);
  cc.ExecuteQuery();           // works fine           
  </code></pre>
  
  <p>I'd also note that when I log in via the browser as global admin to view each user's MySite, I can't see the list of sub-sites anywhere either, even from the Site Contents page:</p>
  
  <p><a href="https://contoso-my.sharepoint.com/personal/j_citizen_contoso_onmicrosoft_com/_layouts/15/viewlsts.aspx" rel="nofollow">https://contoso-my.sharepoint.com/personal/j_citizen_contoso_onmicrosoft_com/_layouts/15/viewlsts.aspx</a></p>
  
  <p>Shows the various libraries in the root site, but not the subsites! (and interestingly, even as global admin, I don't seem to have the option to create any libraries/subsites here)</p>
  
  <p>Update: of course then I thought to check whether the global administrator was actually an administrator for the MySite site collection for each user, and it turns out they weren't. Once added everything works fine. But this means it's only possible to download all content from all user's MySites if somebody has already gone into to each MySite and ensured that they have a shared site administrator, which is going to be a problem.</p>