> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ソーシャル リレーションシップの取得

> XblSocialGetSocialRelationshipsAsync を使用して XBOX Live ユーザーのフレンドとその公開プロパティを取得する、XSAPI フラット C のサンプル コードです。

<Note>このトピックでは、高度な API 利用方法を紹介しています。Social Manager でサポートされていないシナリオを見つけた場合は、開発者アカウント マネージャー (DAM) にご連絡ください。</Note>

このトピックでは、Social Manager API を使用してユーザーのソーシャル リレーションシップとその公開プロパティを取得する方法を示すコード例を提供します。

Social Manager を使い始めるには、「[ソーシャル マネージャーの概要](/services/xbox-services/community/social-manager/live-social-manager-overview)」を参照してください。
Social Manager API を使用すると、オンラインのフレンドとそのゲーム アクティビティを追跡するための開発を大幅に簡素化できます。

## 最初のユーザーのソーシャル リレーションシップを取得する

以下のコード例は、XBOX services でソーシャル リレーションシップを取得する方法を示しています。

このコード例では、以下を行います。

1. システム上のすべてのユーザーのリストを生成し、最初のユーザーを取得します。
2. そのユーザーのすべてのソーシャル リレーションシップを取得します。
3. それらの各リレーションシップの公開プロパティを表示します。

**フラット C API**

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>(); 
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
    HRESULT hr = XblSocialGetSocialRelationshipsResult(asyncBlock, &state.socialResultHandle);

    // Be sure to call XblSocialRelationshipResultCloseHandle when the result object is no longer needed.
};

HRESULT hr = XblSocialGetSocialRelationshipsAsync(
    xboxLiveContext,
    xboxUserId,
    socialRelationshipFilter,
    0,
    0,
    asyncBlock.get()
);

if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}
```

詳細については、以下を参照してください。

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblSocialGetSocialRelationshipsAsync](/reference/live/xsapi-c/social_c/functions/xblsocialgetsocialrelationshipsasync)
* [XblSocialGetSocialRelationshipsResult](/reference/live/xsapi-c/social_c/functions/xblsocialgetsocialrelationshipsresult)
* [XblSocialRelationshipResultCloseHandle](/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultclosehandle)

## 単一のリレーションシップを取得する

**フラット C API**

```cpp theme={null}
const XblSocialRelationship* relationships = nullptr;
size_t relationshipsCount = 0;
HRESULT hr = XblSocialRelationshipResultGetRelationships(state.socialResultHandle, &relationships, &relationshipsCount);

LogToFile("Got %u SocialRelationships:", relationshipsCount);
for (size_t i = 0; i < relationshipsCount; ++i)
{
    LogToFile("Xuid = %u, isFollowingCaller = %u", relationships[i].xboxUserId, relationships[i].isFollowingCaller);
}
```

詳細については、以下を参照してください。

* [XblSocialRelationship](/reference/live/xsapi-c/social_c/structs/xblsocialrelationship)
* [XblSocialRelationshipResultGetRelationships](/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultgetrelationships)

## リレーションシップの次のページを取得する

**フラット C API**

```cpp theme={null}
bool hasNext{ false };
HRESULT hr = XblSocialRelationshipResultHasNext(state.socialResultHandle, &hasNext);

if (hasNext)
{
    auto asyncBlock = std::make_unique<XAsyncBlock>(); 
    asyncBlock->queue = queue;
    asyncBlock->context = nullptr;
    asyncBlock->callback = [](XAsyncBlock* asyncBlock)
    {
        std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
        // Close the handle to the previous page of results.
        if (state.socialResultHandle)
        {
            XblSocialRelationshipResultCloseHandle(state.socialResultHandle);
        }
        HRESULT hr = XblSocialRelationshipResultGetNextResult(asyncBlock, &state.socialResultHandle);
    };

    uint32_t maxItems = 100;
    HRESULT hr = XblSocialRelationshipResultGetNextAsync(xboxLiveContext, state.socialResultHandle, maxItems, asyncBlock.get());
    if (SUCCEEDED(hr))
    {
        // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
        // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
        asyncBlock.release();
    }
}
```

詳細については、以下を参照してください。

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblSocialRelationshipResultCloseHandle](/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultclosehandle)
* [XblSocialRelationshipResultGetNextAsync](/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultgetnextasync)
* [XblSocialRelationshipResultGetNextResult](/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresultgetnextresult)
* [XblSocialRelationshipResultHasNext](/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresulthasnext)


## Related topics

- [ピープル システム (フレンド リスト) のサンプル コード](/ja-jp/services/xbox-services/community/people-system/how-to/live-pplsys-howto-nav.md)
- [ハウツー](/ja-jp/services/xbox-services/community/people-system/how-to/index.md)
- [ソーシャル リレーションシップ変更の処理](/ja-jp/services/xbox-services/community/people-system/how-to/live-handling-a-relationship-change.md)
- [製品リレーションシップ管理](/ja-jp/publishing/game-publishing/concepts/relationship-management.md)
- [XblSocialRelationshipResultHasNext](/ja-jp/reference/live/xsapi-c/social_c/functions/xblsocialrelationshipresulthasnext.md)
