Files icon indicating copy to clipboard operation
Files copied to clipboard

Support for multiple OneDrive "Commercial" accounts

Open AndyMDoyle opened this issue 5 years ago • 15 comments

I have Office 365 accounts with multiple organisations so Explorer shows these all in the sidebar clearly with the company name. Contrast this with Files that shows a single "OneDrive Commercial" entry as shown in this screenshot:

image

I would really like to see "OneDrive Commercial" replaced with however many "OneDrive - Tenant name" entries shown in Explorer.

Thanks.

AndyMDoyle avatar Jan 16 '21 19:01 AndyMDoyle

[Internal comment] To detect onedrive we currently check the "%onedrive%", "%onedrivecommercial%" evironment variables, but this doesn't work in case of multiple business accounts. Onedrive lists all synced accounts in the registry under HKCU\SOFTWARE\Microsoft\OneDrive\Accounts For example the sync folder path can be in HKCU\Software\Microsoft\OneDrive\Accounts\Business1 -> UserFolder I don't have a onedrive for business account to check unfortunately.

gave92 avatar Jan 16 '21 20:01 gave92

@gave92, thanks for the quick reply.

The following code would correctly identify and add all OneDrive accounts (Personal and Business), however due to the sandbox environment a universal app runs in, access to these registry keys in not allowed.

using Files.Enums;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Security;
using System.Threading.Tasks;

namespace Files.Filesystem.Cloud.Providers
{
    public class OneDriveCloudProvider : ICloudProviderDetector
    {
        public async Task DetectAsync(List<CloudProvider> cloudProviders)
        {
            try
            {
                await Task.Run(() =>
                {
                    try
                    {
                        var oneDriveAccountsKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts", false);

                        foreach (var account in oneDriveAccountsKey.GetSubKeyNames())
                        {
                            var accountKeyName = @$"{oneDriveAccountsKey.Name}\{account}";
                            var displayName = (string)Registry.GetValue(accountKeyName, "DisplayName", null);
                            var userFolder = (string)Registry.GetValue(accountKeyName, "UserFolder", null);

                            if (!string.IsNullOrWhiteSpace(displayName) && !string.IsNullOrWhiteSpace(userFolder))
                            {
                                cloudProviders.Add(new CloudProvider()
                                {
                                    ID = CloudProviders.OneDrive,
                                    Name = $"OneDrive - {displayName}",
                                    SyncFolder = userFolder
                                });
                            }
                        }
                    }
                    catch (SecurityException) { }
                });
            }
            catch
            {
                // Not detected
            }
        }
    }
}

I haven't dug around the source code too much yet, but I see Files.Launcher is able to receive commands and reply with data. If the OneDriveCloudProvider could access the AppServiceConnection could the Registry query be performed by Files.Launcher and a list of key value pairs representing the display name and user folder be returned?

With such an implementation, the OneDriveCommercialCloudProvider can be removed completely.

AndyMDoyle avatar Jan 16 '21 21:01 AndyMDoyle

Ah pity, read-only access is usually allowed :/ I see two solutions:

  • The path is also saved in %localappdata%\Microsoft\OneDrive\settings\Personal -> in the first .ini file
  • Try and see if the code you posted works in Files.Launcher -> it may have the same limitation

gave92 avatar Jan 16 '21 21:01 gave92

@AndyMDoyle I just tried your code and works perfectly on my PC xD Do you get SecurityException also when running the app in Debug mode? Or in Release only? (I've tested debug only)

gave92 avatar Jan 16 '21 21:01 gave92

I got the exception in Debug mode when launched from VS.

Maybe it's the debugging that is the issue... let me try in release mode without debugging.

AndyMDoyle avatar Jan 16 '21 21:01 AndyMDoyle

N

Mohammedalgcham avatar Jan 16 '21 22:01 Mohammedalgcham

@gave92, switching to Release mode and launching Files.Package is taking a very long time to build and deploy and for the first time I'm seeing my Ryzen 3900XT fully maxed out for now.

Should it take a long time in release mode?

In debug mode with VS in normal and elevated to admin I don't see any OneDrive entries in the sidebar and it hits a breakpoint on the catch (SecurityException).

Glad it works on your machine though and shows OneDrive - Personal at least.

AndyMDoyle avatar Jan 16 '21 22:01 AndyMDoyle

Should it take a long time in release mode?

Yes :( If you don't want to stress your pc you can open a draft PR and let azure do the job. This would also allow other contributors to test if the code works for them.

gave92 avatar Jan 16 '21 22:01 gave92

Good suggestion, thanks @gave92

PR #3099 is underway.

AndyMDoyle avatar Jan 16 '21 22:01 AndyMDoyle

@gave92 I have opened a new case #3151 for additional synced locations. This is a little bit different from the topic discussed here, but maybe it can be put together. We have two scenarios:

  1. Connect OneDrive with your first M365 identity. You will get your OneDrive folder synced on your device. After that, connect a 2nd M365 identity with your OneDrive client, then you have two OneDrive folders as you can see in Andys screenshot.
  2. You connect your single M365 identity, after that you go to your SharePoint and sync some libraries. Additionally, you can go to a different M365 tenant and you can sync folder from this location as well. The result will look like in my closed bug. Screenshot 2021-01-18 210810

I have put together both use cases in one screenshot. In the upper part you can see two locations with the office building symbol, these are libraries from SharePoint. Below you can see the personal OneDrive from my first identity, following by the personal OneDrive from my second identity.

All root folders are located by default in the user home folder, but in the registry they are handled in a different way. You can see your mounted folders in HKEY_CURRENT_USER\Software\Microsoft\OneDrive\Accounts\Business1\ScopeIdToMountPointPathCache

heikobischoff avatar Jan 18 '21 20:01 heikobischoff

@AndyMDoyle the above looks like another point in favor for implementing this by reading the registry and not the .ini files xD

gave92 avatar Jan 18 '21 20:01 gave92

I just checked my %localappdata%\Microsoft\OneDrive\settings\ folder. I have three folders: Business1, Business2 and Personal. In Business1 I discovered a file "aRandomGuid.ini" with some "libraryScope"s and "libraryFolder"s. The library folders are my mapped SharePoint locations. Maybe you can read all libraryFolders for all business accounts (you can have x synced locations for y business accounts).

heikobischoff avatar Jan 18 '21 20:01 heikobischoff

@gave92, did you see the update to PR #3099 I posted yesterday? It includes all of the required changes to support this now and to allow each ICloudProviderDetector to load and populate the sidebar in the background (which was needed to allow time for the app service connection to become available without delaying the app startup)

Adding support for sync'd SharePoint sites should not be a problem either.

AndyMDoyle avatar Jan 18 '21 21:01 AndyMDoyle

@AndyMDoyle not yet but it looks the right path to go forward as it also allows to solve other issues such as recyclebin appearing empty if navigating to it too early after startup.

Edit: @AndyMDoyle I like the approach you propose, if you don't mind I'm going to do a couple of minor changes and open a PR to #3099 👍

gave92 avatar Jan 18 '21 21:01 gave92

@AndyMDoyle Thank you for implementing this feature.

yair100 avatar Jan 20 '21 15:01 yair100