Get Inventory Items from the Marketplace

The following code sample shows how to retrieve a list of inventory items.

The sample calls the asynchronous method InventoryService.GetInventoryItemsAsync Method to get the list of inventory items. As the system returns the list of items, this example appends the items to an array provided by the code that calls the function in this example.

    UINT Sample::GetInventoryItems( __in XboxLiveContext^ Requester,
                                    __in Marketplace::MediaItemType itemType,
                                    __inout IVector<Marketplace::InventoryItem^>^ InventoryItems)
    {
        UINT itemsFound = 0;
        IAsyncOperation<Marketplace::InventoryItemsResult^>^ pAsyncOp =
            Requester->InventoryService->GetInventoryItemsAsync( itemType );

        create_task( pAsyncOp )
            .then( [this, &InventoryItems, &itemsFound] ( task< Marketplace::InventoryItemsResult^ > inventoryItemsTask )
        {
            try
            {
                Marketplace::InventoryItemsResult^ inventoryResult = inventoryItemsTask.get();
                //  Because we may be compiling a list of multiple content types from separate
                //  calls, we just append the results to the passed in vector.
                critical_section::scoped_lock lock( m_InventoryListsLock );
                for( itemsFound = 0; itemsFound < inventoryResult->Items->Size; ++itemsFound )
                {
                    InventoryItems->Append( inventoryResult->Items->GetAt( itemsFound ) );
                }
            }
            catch (Platform::Exception^ ex)
            {
                XboxSampleFramework::DebugPrint( "SAMPLE: Error getting inventory : 0x%x\n", ex->HResult);
                m_hrDisplayError = ex->HResult;
                m_appState = APP_DONE_ERROR;
            }
        })
        .wait();

        return itemsFound;
    }