Language: HTML | XAML

How to receive HTML (XAML)

Applies to Windows and Windows Phone

Sharing HTML content is different from sharing other basic formats such as text or a link. HTML content can consist of a variety of content, including text, images, and other information. Here's how your app can receive the HTML content a user wants to share.

When you add this functionality to your app, consider also accepting content in a text format. Most apps that allow users to share HTML also include text as a secondary format. Adding support for the text format helps ensure that your app is available to users more often.

What you need to know

Technologies

Prerequisites

  • You should be familiar with Visual Studio and its associated templates.
  • You should be familiar with C# development.

Instructions

Step 1: Support the Share contract

Before your app can receive shared content, you have to declare that it supports the Share contract. This contract essentially lets the system know that your app is available to receive content. If you're using a Microsoft Visual Studio template to create your app, here's how you support the Share contract:

  1. Open the manifest file. It should be called something like package.appxmanifest.
  2. Open the Declarations tab.
  3. Choose Share Target from the Available Declarations list.

Step 2: Specify that your app supports HTML

To specify that your app supports HTML as a data format:

  1. Open the manifest file.
  2. In the Data Formats section, click Add New.
  3. Type html. Don't include the period (.).

Step 3: Adding the necessary namespaces

For a target app, you'll need the Windows.ApplicationModel.Activation, Windows.ApplicationModel.DataTransfer, and Windows.ApplicationModel.DataTransfer.ShareTarget namespaces:


using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.DataTransfer.ShareTarget;


Step 4: Handling share activation

When a user selects your app to share content, the system activates your app by calling the app's Application.OnShareTargetActivated method. You need to override this method to get the content that a user wants to share.


protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
    // Code to handle activation goes here.	
}

Step 5: Get the ShareOperation object.

The ShareOperation object contains all the data that your app needs to get the content that a user wants to share.


ShareOperation shareOperation = args.ShareOperation;

Step 6: Check to see if the DataPackageView contains HTML.

The ShareOperation object contains a DataPackageView object. This object is essentially a read-only version of the DataPackage object that the source app used to create the data. Use this object to see if the content being shared is available in HTML format.


if (shareOperation.Data.Contains(StandardDataFormats.Html))
{
    // Code to process HTML goes here.
}


Even if your app supports only HTML, you should add a statement similar to the preceding code. It makes it easier to support other data types and file formats later if you choose.

Step 7: Process the HTML.

To get the HTML, call the DataPackageView.GetHtmlFormatAsync method. Also, be aware that HTML from another app is not trusted. As a security precaution, you shouldn't display HTML unless you're sure it doesn't have any dynamic content. You can use the DataTransfer.HtmlFormatHelper.GetStaticFragment method to get shared HTML content without any dynamic elements such as script tags.


ShareOperation shareOperation = args.ShareOperation;
if (shareOperation.Data.Contains(StandardDataFormats.Html))
{
    string htmlFormat = await shareOperation.Data.GetHtmlFormatAsync();
    string htmlFragment = HtmlFormatHelper.GetStaticFragment(htmlFormat);

    // To output text from this example, you need a TextBlock control
    // with a name of "contentValue". 
    contentValue.Text += "HTML: " + Environment.NewLine;
    this.webView.Visibility = Visibility.Visible;
    this.webView.NavigateToString("<html><body>" + htmlFragment + "</body></html>");
}

Of course, what you do with the text depends on your app.

Step 8: Call reportCompleted.

After your app finishes processing the shared content, call ReportCompleted. You must call this method so the system knows your app is no longer needed.


shareOperation.reportCompleted();


Note  If you're going to test the code in this section, we recommend that you do not immediately add the code that calls ReportCompleted. This is because the system closes your app after ReportCompleted is called, which prevents the test from proceeding. When you move your code into production, make sure you add the call to ReportCompleted.

Remarks

The best way to see how an app can receive HTML as part of sharing is by checking out our code sample. However, the following example should also give you a good idea of how to write an if statement that detects whether a user is trying to share HTML through your app.

Complete example



if (shareOperation.Data.Contains(StandardDataFormats.Html))
{
    string htmlFormat = await shareOperation.Data.GetHtmlFormatAsync();
    string htmlFragment = HtmlFormatHelper.GetStaticFragment(htmlFormat);

    // To output text from this example, you need a TextBlock control
    // with a name of "contentValue". 
    contentValue.Text += "HTML: " + Environment.NewLine;
    this.webView.Visibility = Visibility.Visible;
    this.webView.NavigateToString("<html><body>" + htmlFragment + "</body></html>");
}

Related topics

Sharing content target app sample
Sharing and exchanging data
How to receive a link
How to receive text
Quickstart: Receiving shared content
DataPackage
Windows.ApplicationModel.DataTransfer
Windows.ApplicationModel.DataTransfer.Share
Guidelines for debugging target apps

 

 

Show:
© 2014 Microsoft