Users often want to share files with other people or apps. Using the sharing feature, your app can easily support tasks ranging from emailing a file to a colleague to sharing a set of pictures from a family vacation.

Sharing in Windows Phone 8

Sharing in Windows Phone 8

Sharing was a new concept in Windows 8 with they introduced Share contract charms. But for Windows Phone 7 and 8, Microsoft gave a limited functionity called “Share Task” under Microsoft.Phone.Tasks. Under this one users were able to share, status, links and media only. But the leaked Windows Phone 8.1 SDK suggests, there is more to come under “Sharing” in Windows Phone 8.1 and its just like sharing we have experienced in Windows 8.

As thr SDK documentation says, developers need to be familiar with Windows.ApplicationModel.DataTransfer namespace and must have experiences in how to get files and other data, such as by using FileOpenPicker.

This is the step by step guide to perform file sharing in WP 8.1

Step 1

You need to add the right namespaces to your app so you can create and process the objects related to sharing. At a minimum, you should add the Windows.ApplicationModel.DataTransfer namespace

using namespace Windows::ApplicationModel::DataTransfer;

This namespace has all you need for basic sharing. Remember, though, if you want to share content such as images or files, you’ll need to add those namespaces as well. Here’s a list of the namespaces you might need:

  • Windows.Storage. Needed for working with StorageFile and other objects.
  • Windows.Storage.Pickers. Used to open the file picker so users can select images and files.
  • Windows.Storage.Streams. Often used when sharing images, files, and custom-formatted data.
  • Windows.Graphics.Imaging. Useful if you need to modify images before sharing them.

Step 2

Get the DataTransferManager object, that is the starting point of any sharing operation.
DataTransferManager^ dataTransferManager = DataTransferManager::GetForCurrentView();

Step 3

Add a DataRequested event handler to fire when the user wishes to invoke Share. If you’re writing a Windows Store app, this will occur automatically when the user invokes the Share charm. If you’re developing for Windows Phone, there is no built-in Share charm, so you’ll need to add a control for the user to tap and trigger the handler.
auto dataRequestedToken = dataTransferManager->DataRequested += ref new TypedEventHandler<DataTransferManager^, DataRequestedEventArgs^>(this, &MainPage::ShareStorageItemsHandler);

Step 4

When a DataRequested event occurs, your app receives a DataRequest object. This object contains a DataPackage that you can use to provide the content that the user wants to share.
DataRequest^ request = e->Request;

Step 5

Set the title and description properties
request->Data->Properties->Title = "Share StorageItems Example";
request->Data->Properties->Description = “Demonstrates how to share files.”;

Step 6

Finally, we have to add the files to the DataPackage using SetStorageItems method

These are the 6 necessary steps. Since we are using C++, all method invocation and class initializations will be faster then compared to managed C# code.

Please note that, if you want a target app to have read-write capabilities on the StorageItems, use the overloaded version of the SetStorageItems method that lets you specify read-only or read-write. For example, a source app that asks the target to perform a delete-on-paste operation would specify read-write so enable the target to delete the source files after copying them.

The below is the full code

void MainPage::RegisterForShare()
    DataTransferManager^ dataTransferManager = DataTransferManager::GetForCurrentView();
    auto dataRequestedToken = dataTransferManager->DataRequested += 
        ref new TypedEventHandler<DataTransferManager^, DataRequestedEventArgs^>(
            this, &MainPage::ShareStorageItemsHandler);

void MainPage::ShareStorageItemsHandler(DataTransferManager^ sender, 
    DataRequestedEventArgs^ e)
    DataRequest^ request = e->Request;
    request->Data->Properties->Title = "Share StorageItems Example";
    request->Data->Properties->Description = "Demonstrates how to share files.";

    // Because we are making async calls in the DataRequested event handler,
    // we need to get the deferral first.
    DataRequestDeferral^ deferral = request->GetDeferral();  

         [this, request, deferral](task<StorageFile^> getFileTask)
            auto logoFile = getFileTask.get();
            auto storageItems = ref new Vector<IStorageItem^>();
        catch (Exception^ ex)
            // Calling FailWithDisplayText() also calls Complete on the deferral.

Happy coding folks