Microsoft Graph SDK C#: Use FileSavePicker with Onedrive

369 views Asked by At

Is it possible to use a FileSavePicker object to save a file in Onedrive using the Microsoft Graph SDK for C#.

The reason is that i need the user to select where the data backup file will be stored.

Using the following source code i am able to download or upload a file but without prompting the user to select a desired path.

Authentication.cs

internal class Authentication
{

    internal static GraphServiceClient GetAuthenticatedClient()
    {
        if (Settings.graphClient == null)
        {
            try
            {
                Settings.graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(
                            async (requestMessage) =>
                            {
                                var token = await GetTokenForUserAsync();

                                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                            })
                        );

                return Settings.graphClient;
            }
            catch (Exception)
            { }
        }
        return Settings.graphClient;
    }


    internal static async Task<string> GetTokenForUserAsync()
    {
        AuthenticationResult authResult;

        try
        {
            authResult = await Settings.IdentityClientApp.AcquireTokenSilentAsync(Settings.Scopes);

            Settings.TokenForUser = authResult.Token;
        }
        catch (Exception)
        {
            if (Settings.TokenForUser == null || Settings.Expiration <= DateTimeOffset.UtcNow.AddMinutes(5))
            {
                authResult = await Settings.IdentityClientApp.AcquireTokenAsync(Settings.Scopes);

                Settings.TokenForUser = authResult.Token;

                Settings.Expiration = authResult.ExpiresOn;
            }
        }

        return Settings.TokenForUser;
    }


    internal static void SignOut()
    {
        foreach (var user in Settings.IdentityClientApp.Users)

            user.SignOut();

        Settings.graphClient = null;

        Settings.TokenForUser = null;
    }

}

Settings.cs

internal class Settings
{
    public static string clientId = "xxxxxx-xxxx-xxx-xxx-xxxxxxxxx";

    public static string[] Scopes = {
                                        "https://graph.microsoft.com/User.Read",
                                        "https://graph.microsoft.com/Mail.Send",
                                        "https://graph.microsoft.com/Files.ReadWrite.All",
                                        "https://graph.microsoft.com/Files.ReadWrite" };

    public static PublicClientApplication IdentityClientApp = new PublicClientApplication(clientId);

    public static string TokenForUser = null;

    public static DateTimeOffset Expiration;

    public static GraphServiceClient graphClient = null;
}

Download File

        public static async Task<bool> Download(string file, string to)
        {
            try
            {
                var download = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(file), Uri.EscapeUriString(System.IO.Path.GetFileName(file)));

                using (Stream stream = await Authentication.GetAuthenticatedClient().Me.Drive.Root.ItemWithPath(download).Content.Request().GetAsync())
                {
                    try
                    {
                        using (FileStream writer = new FileStream(to, FileMode.Create))
                        {

                        }

                        using (StreamReader reader = new StreamReader(stream))
                        {
                            await PathIO.WriteTextAsync(to, reader.ReadToEnd());
                        }

                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
1

There are 1 answers

0
Dave Smits On BEST ANSWER

Your question is not totally clear to me; I understand that you want to use the FilePicker with the OneDrive app and then force the user selects a target in OneDrive?

This is not possible. I think the best approach would be build your own UI to let the user select a folder and use the code you have above to save the backup file to onedrive