Search code examples
xamarin.formsapp-configflurl

Downloading file from SharePoint with Flurl causes "Configuration system failed to initialize" error


I'm using Flurl to download a File from SharePoint. However, it causes an "Configuration system failed to initialize" error.

Here is my code:

 var url = "https://mysite.sharepoint.com"
                .AppendPathSegment("/_api/web/Shared%20Documents/test.xlsx")
                .SetQueryParams(new
                {
                    api_key = ConfigurationManager.AppSettings["MYAPIKEY"],
                    max_results = 20,
                    q = "Don't worry, I'll get encoded!"
                })
                .SetFragment("after-hash");

                //returns fullpath for App-specific storage, where inventory data is stored
                string fullpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

                var path = await "https://mysite.sharepoint.com/Shared%20Documents/test.xlsx"
                    .DownloadFileAsync(fullpath);

The API key I added was generated on Azure AD.

I suspect that the error is caused by my app.config file, however because I'm using Xamarin.Forms, I did not originally have an app.config file. To solve this, I'm using the PCLAppConfig library and I followed the description in the link.

My problem is, I am not sure on what i should put inside the app.config file. The current content of the config file is below and I've tried a few different things, but I always got the same error.

<configuration>
  <appSettings>
    <add key="api_key" value="MYAPIKEY" />
  </appSettings>
</configuration>

This is the call stack, in case it helps:

>   0x3F in GraphTutorial.DownloadFile.Flurlcall at D:\Schule\Diplomarbeit\GraphTutorial\CalendarPage.xaml.cs:35,17 C#
    0x33 in System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start<GraphTutorial.DownloadFile.<Flurlcall>d__1>    C#
    0x38 in GraphTutorial.DownloadFile.Flurlcall    C#
    0x11 in Xamarin.Forms.Button.Xamarin.Forms.Internals.IButtonElement.PropagateUpClicked at D:\a\1\s\Xamarin.Forms.Core\Button.cs:177,47  C#
    0x20 in Xamarin.Forms.ButtonElement.ElementClicked at D:\a\1\s\Xamarin.Forms.Core\ButtonElement.cs:61,5 C#
    0x2 in Xamarin.Forms.Button.SendClicked at D:\a\1\s\Xamarin.Forms.Core\Button.cs:163,32 C#
    0x4 in Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick at D:\a\1\s\Xamarin.Forms.Platform.Android\ButtonElementManager.cs:25,4  C#
    0xD in Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick at D:\a\1\s\Xamarin.Forms.Platform.Android\FastRenderers\ButtonRenderer.cs:71,45 C#
    0x13 in Android.Views.View.IOnClickListenerInvoker.n_OnClick_Landroid_view_View_    C#
    0x17 in Android.Runtime.DynamicMethodNameCounter.48 C#

Solution

  • Turns out the error has nothing to do with Flurl itself, the url I used was the actual problem.

    I managed to make it work by calling the Microsoft Graph API with this query instead:

    GET https://graph.microsoft.com/v1.0/sites/root/drive/root:/Subfolder/test.xlsx

    In the response you get the property @microsoft.graph.downloadUrl which allows you to download the url.

    Here is a link that helped me a lot in figuring this out: https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http