Bewerken

Share via


Get product info for apps and add-ons

This article demonstrates how to use methods of the StoreContext class in the Windows.Services.Store namespace to access Store-related info for the current app or one of its add-ons.

For a complete sample application, see the Store sample.

Note

The Windows.Services.Store namespace was introduced in Windows 10, version 1607, and it can only be used in projects that target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio. If your app targets an earlier version of Windows 10, you must use the Windows.ApplicationModel.Store namespace instead of the Windows.Services.Store namespace. For more information, see this article.

Prerequisites

These examples have the following prerequisites:

  • A Visual Studio project for a Universal Windows Platform (UWP) app that targets Windows 10 Anniversary Edition (10.0; Build 14393) or a later release.
  • You have created an app submission in Partner Center and this app is published in the Store. You can optionally configure the app so it is not discoverable in the Store while you test it. For more information, see our testing guidance.
  • If you want to get product info for an add-on for the app, you must also create the add-on in Partner Center.

The code in these examples assume:

  • The code runs in the context of a Page that contains a ProgressRing named workingProgressRing and a TextBlock named textBlock. These objects are used to indicate that an asynchronous operation is occurring and to display output messages, respectively.
  • The code file has a using statement for the Windows.Services.Store namespace.
  • The app is a single-user app that runs only in the context of the user that launched the app. For more information, see In-app purchases and trials.

Note

If you have a desktop application that uses the Desktop Bridge, you may need to add additional code not shown in these examples to configure the StoreContext object. For more information, see Using the StoreContext class in a desktop application that uses the Desktop Bridge.

Get info for the current app

To get Store product info about the current app, use the GetStoreProductForCurrentAppAsync method. This is an asynchronous method that returns a StoreProduct object that you can use to get info such as the price.

private StoreContext context = null;

public async void GetAppInfo()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Get app store product details. Because this might take several moments,   
    // display a ProgressRing during the operation.
    workingProgressRing.IsActive = true;
    StoreProductResult queryResult = await context.GetStoreProductForCurrentAppAsync();
    workingProgressRing.IsActive = false;

    if (queryResult.Product == null)
    {
        // The Store catalog returned an unexpected result.
        textBlock.Text = "Something went wrong, and the product was not returned.";

        // Show additional error info if it is available.
        if (queryResult.ExtendedError != null)
        {
            textBlock.Text += $"\nExtendedError: {queryResult.ExtendedError.Message}";
        }

        return;
    }

    // Display the price of the app.
    textBlock.Text = $"The price of this app is: {queryResult.Product.Price.FormattedBasePrice}";
}

Get info for add-ons with known Store IDs that are associated with the current app

To get Store product info for add-ons that are associated with the current app and for which you already know the Store IDs, use the GetStoreProductsAsync method. This is an asynchronous method that returns a collection of StoreProduct objects that represent each of the add-ons. In addition to the Store IDs, you must pass a list of strings to this method that identify the types of the add-ons. For a list of the supported string values, see the ProductKind property.

Note

The GetStoreProductsAsync method returns product info for the specified add-ons that are associated with the app, regardless of whether the add-ons are currently available for purchase. To retrieve info for all the add-ons for the current app that can currently be purchased, use the GetAssociatedStoreProductsAsync method as described in the following section instead.

This example retrieves info for durable add-ons with the specified Store IDs that are associated with the current app.

private StoreContext context = null;

public async void GetProductInfo()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable" };
    List<String> filterList = new List<string>(productKinds);

    // Specify the Store IDs of the products to retrieve.
    string[] storeIds = new string[] { "9NBLGGH4TNMP", "9NBLGGH4TNMN" };

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult =
        await context.GetStoreProductsAsync(filterList, storeIds);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        // Access the Store info for the product.
        StoreProduct product = item.Value;

        // Use members of the product object to access info for the product...
    }
}

Get info for add-ons that are available for purchase from the current app

To get Store product info for the add-ons that are currently available for purchase from the current app, use the GetAssociatedStoreProductsAsync method. This is an asynchronous method that returns a collection of StoreProduct objects that represent each of the available add-ons. You must pass a list of strings to this method that identify the types of add-ons you want to retrieve. For a list of the supported string values, see the ProductKind property.

Note

If the app has many add-ons that are available for purchase, you can alternatively use the GetAssociatedStoreProductsWithPagingAsync method to use paging to return the add-on results.

The following example retrieves info for all durable add-ons, Store-managed consumable add-ons, and developer-managed consumable add-ons that are available for purchase from the current app.

private StoreContext context = null;

public async void GetAddOnInfo()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable", "Consumable", "UnmanagedConsumable" };
    List<String> filterList = new List<string>(productKinds);

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult = await context.GetAssociatedStoreProductsAsync(filterList);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        // Access the Store product info for the add-on.
        StoreProduct product = item.Value;

        // Use members of the product object to access listing info for the add-on...
    }
}

Get info for add-ons for the current app that the user has purchased

To get Store product info for add-ons that the current user has purchased, use the GetUserCollectionAsync method. This is an asynchronous method that returns a collection of StoreProduct objects that represent each of the add-ons. You must pass a list of strings to this method that identify the types of add-ons you want to retrieve. For a list of the supported string values, see the ProductKind property.

Note

If the app has many add-ons, you can alternatively use the GetUserCollectionWithPagingAsync method to use paging to return the add-on results.

The following example retrieves info for durable add-ons with the specified Store IDs.

private StoreContext context = null;

public async void GetUserCollection()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable" };
    List<String> filterList = new List<string>(productKinds);

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult = await context.GetUserCollectionAsync(filterList);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        StoreProduct product = item.Value;

        // Use members of the product object to access info for the product...
    }
}