Open Share Dialog from Universal Windows Javascript App (UWP)

Opening Share Dialog Popup in Windows Universal App is very simple and with a few lines of code, this functionality can be quickly added to any app.

The code below can be used to share an app’s web URL (you can use MS Store URL of the app as well) in UWP Javascript application

var page = WinJS.UI.Pages.define("/index.html", {
    ready: function (element, options) {

        var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
        dataTransferManager.addEventListener("datarequested", dataRequested);

    },
    unload: function () {
        var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
        dataTransferManager.removeEventListener("datarequested", dataRequested);
    }

});

function dataRequested(e) {
    var request = e.request;
    var storeID = 'xxxxxxxxxxxx';
    request.data.properties.title = "Your title";
    request.data.properties.description = "App description";
    request.data.setWebLink(new Windows.Foundation.Uri("https://www.microsoft.com/store/apps/" + storeID));
}

function showShareUI() {
    Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI();
}

In the application page ready event we need to initialize DataTransferManager and attach an event listener for “datarequested”. In that method you can set title, display, web link properties which will be used by the dialog popup. Store ID of the app can be found from “App Management -> App Identity” page of the specific app in your developer account. In order to launch Share Dialog Popup simply call “showShareUI” method on some action (such as some button click etc). This will open dialog box as following

UWP App Share Dialog

Share Dialog Popup displays the title and description at the top. When you select an app from the dialog and open it, web link (which was set in the dataRequested event) is copied in the selected target app.


Leave A Comment

Your email address will not be published. Required fields are marked *