MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
452 stars 55 forks source link

Disable Built-in Office File Viewer in WebView2 Winforms #1959

Closed mrgucci1 closed 2 years ago

mrgucci1 commented 2 years ago

I've noticed in the past few days when I try to download an Excel file this new window appears with a preview.

officelive

Is there a way to disable this within WebView2? I see there is a setting for it in Edge, but my googling for a WebView2 equivalate has come up short.

How to disable in Edge: https://www.askvg.com/tip-disable-built-in-office-file-viewer-in-microsoft-edge/

AB#37158967

champnic commented 2 years ago

Hey @mrgucci1 - I don't think there's currently a way to disable this in WebView2. I'm going to turn this into a bug as I believe this should be off by default for WebView2. Thanks!

mrgucci1 commented 2 years ago

Hey @mrgucci1 - I don't think there's currently a way to disable this in WebView2. I'm going to turn this into a bug as I believe this should be off by default for WebView2. Thanks!

Awesome, thank you so much @champnic

chanrithdeepsearch commented 2 years ago

What is the current status of this issue? Is there a way we can work around that?

@champnic

champnic commented 2 years ago

We have this assigned to a dev, but I don't believe they've begun work on this yet.

You might be able to workaround by handling the NavigationStarting event and cancel navigations to view.officeapps.live.com.

nishitha-burman commented 2 years ago

Hey @mrgucci1 and @chanrithdeepsearch, we were curious why you want to disable this popup and what user flow you expect for downloading the Excel file?

Also, we are looking into this bug further but for now you can use this flag msOpenOfficeDocumentsInWebViewer to disable the popup.

Thank you!

mrgucci1 commented 2 years ago

Hey @nishitha-burman

I wanted to disable it because it interfered with some automation I had running in WebView2, I wanted to download an excel file but instead of downloading the file the new window appeared with the preview. I wasnt able to handle the popup with the NewWindowRequested event either, so I created the issue to see if there was a way to disable it!

The user flow I expect would just be when a user clicks to download an excel file it downloads straight away without the preview appearing. I could see how the preview is useful for a bunch of cases but for mine it got in the way

nishitha-burman commented 2 years ago

Hi @mrgucci1,

Thank you for your reply. Have you tried the msOpenOfficeDocumentsInWebViewer flag? We are looking further into this bug but that flag should work in the meantime :)

Thanks, Nishitha

chanrithdeepsearch commented 2 years ago

@nishitha-burman our client has a lot of protocol regarding web security, and have most of the website block by default. when click on the link to download the file, instead of downloading, it's redirect to view.officeapps.live.com, which is blocked. I am also try looking for the msOpenOfficeDocumentsInWebViewer, but couldn't find any documentation regarding this flag yet.

champnic commented 2 years ago

You can disable that flag when initializing the WebView2: https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2environmentoptions.additionalbrowserarguments?view=webview2-dotnet-1.0.1108.44

var options = new CoreWebView2EnvironmentOptions("--disable-features=msOpenOfficeDocumentsInWebViewer");
var env = await CoreWebView2Environment.CreateAsync(null, null, options);
mrgucci1 commented 2 years ago

For anyone that finds this and is using Winforms, you might encounter this exception:

System.ArgumentException: 'WebView2 was already initialized with a different CoreWebView2Environment. Check to see if the Source property was already set or EnsureCoreWebView2Async was previously called with different values.'

Make sure you do not have your source property set before you add the flag, you might have to go into your From1.Designer.cs file and remove your source

this.webView2.Source = new System.Uri("https://www.google.com", System.UriKind.Absolute);

After you remove from the designer you can apply this flag in your InitializeAsync() method like this:

async Task InitializeAsync()
{
    //create our env with flag applied
    var envOps = new CoreWebView2EnvironmentOptions("--disable-features=msOpenOfficeDocumentsInWebViewer");
    var env = await CoreWebView2Environment.CreateAsync(null, null, envOps);
    //initialize corewebview2 with our new env
    await webView2.EnsureCoreWebView2Async(env);
    //now we can set source property
    webView2.Source=new System.Uri("https://www.google.com", System.UriKind.Absolute);  
}

thank you @champnic and @nishitha-burman!

champnic commented 2 years ago

We have removed the Edge branding from the icon and messaging, and changed the feature to be off by default. If you need the functionality of viewing the document within the WebView2, you can re-enable the flag:

var options = new CoreWebView2EnvironmentOptions("--enable-features=msOpenOfficeDocumentsInWebViewer");
var env = await CoreWebView2Environment.CreateAsync(null, null, options);

This is available in runtimes 105.0.1318.0+. Thanks!

br1an-h commented 2 years ago

HI @champnic,

I am trying to get office files (.xlsx, .docx) to display in the WebView2 control in C++, I have the latest version (1.0.1293.44) installed, but when I add the environment option to enable this feature it still just downloads the file (doesn't even prompt first).

Here is my code for creating the options:

   auto opts = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
   opts->put_AdditionalBrowserArguments(L"--enable-features=msOpenOfficeDocumentsInWebViewer");
   ICoreWebView2EnvironmentOptions* options = opts.Get();

Is this feature supported in the version I am using, or am I doing something wrong?

Thanks.

champnic commented 2 years ago

Can you verify the command line of the main msedgewebview2.exe browser process? Does it have that flag as expected?

br1an-h commented 2 years ago

Yes, the command line does have the flag, although there are 12 msedgewebview2.exe processes running under my app (my app currently has 4 WebView2 controls open), but they all have the setting, except for the one with "--type=crashpad-handler".

Here is an image of my Task Manager screen:

WebView2-Processes

Not sure if it matters, but I am trying to open a local file, e.g. with the url: "file://C:\Users\brian.QISOFT\Documents\Loss Tracking API.docx"

br1an-h commented 2 years ago

I just tested this on the Edge browser with a local file url, and with this option enabled it too just downloads the file instead of viewing it! :(

br1an-h commented 2 years ago

I have tested with the following urls for sample .docx and .xlsx files, and the viewer works fine with these, but I still can't get it to work for local file urls, is this a bug?

https://view.officeapps.live.com/op/view.aspx?src=https%3A%2F%2Ffile-examples.com%2Fstorage%2Ffe9cbf00c7630dd7a8d6704%2F2017%2F02%2Ffile_example_XLSX_10.xlsx&wdOrigin=BROWSELINK

https://view.officeapps.live.com/op/view.aspx?src=https%3A%2F%2Ffile-examples.com%2Fstorage%2Ffe8bd9dfd063066d39cfd5a%2F2017%2F02%2Ffile-sample_100kB.docx&wdOrigin=BROWSELINK

Thanks,

Brian

champnic commented 2 years ago

Hm - that may be a bug that it only displays for web files. Can you open that as a new GitHub issue?