---
title: "ユーザー登録後スクリプト"
slug: "user-post-registration-script"
abstract: "外部ログインプロバイダーがGoogleの場合、特定のユーザーグループに追加するためのウェブスクリプト例。"
---
# ユーザー登録後スクリプト

この例は、新規ユーザーがSitecore Content Hubに追加された後に実行される *ユーザー登録後* ウェブスクリプトを示しています。外部プロバイダーがGoogleの場合、そのユーザーを特定のユーザーグループに追加します。

> !注**始める前に**
> 
> -   事前登録プロセスで必要なユーザーグループやプロファイルを設定してください。
>     
> -   *Google*というユーザーグループを作成しましょう。
>     
> -   事前登録ワークフローのスクリプトロジックとスキーマプロパティが一致していることを確認してください。
>     

## 文字

using System.Linq;

if (Context.ExternalUserInfo?.Provider != "Google") return;

var query = Query.CreateQuery(entities =>
 from e in entities
 where e.DefinitionName == "Usergroup" && e.Property("GroupName") == "Google"
 select e);

var googleGroupId = await MClient.Querying.SingleIdAsync(query);
if (!googleGroupId.HasValue) throw new InvalidOperationException("Google usergroup not found.");

var relation = await Context.User.GetRelationAsync<IChildToManyParentsRelation>("UserGroupToUser");
relation.Parents.Add(googleGroupId.Value);

await MClient.Entities.SaveAsync(Context.User);

## 脚本の説明

このセクションでは、スクリプトを実行順に順に進め、各パートについて説明します。番号付きの項目はシーケンスを記述しており、実行命令ではありません。

1.  必要なライブラリも含めてください。
    
    using System.Linq;
    
2.  外部ユーザープロバイダーを確認してください。プロバイダーがGoogleしていなければ、スクリプトは終了します。
    
    if (Context.ExternalUserInfo?.Provider != "Google") return;
    
3.  **Googleユーザーグループ**を取得する[クエリ](https://doc.sitecore.com/ch/ja/developers/cloud-dev/linq-queries.html "LINQ queries")を作成します。
    
    var query = Query.CreateQuery(entities =>
    from e in entities
    where e.DefinitionName == "Usergroup" && e.Property("GroupName") == "Google"
    select e);
    
4.  クエリを実行してGoogleグループIDを取得します。
    
    var googleGroupId = await MClient.Querying.SingleIdAsync(query);
    
5.  ユーザーグループが見つからない場合は、InvalidOperationExceptionを投げてください。
    
    if (!googleGroupId.HasValue) throw new InvalidOperationException("Google usergroup not found.");
    
6.  ユーザーのユーザーグループの関係を取得しましょう。
    
    var relation = await Context.User.GetRelationAsync<IChildToManyParentsRelation>("UserGroupToUser");
    
7.  ユーザーグループ関係を通じてGoogleグループをユーザーのオブジェクトに追加します。
    
    relation.Parents.Add(googleGroupId.Value);
    
8.  ユーザーエンティティを保存してください。
    
    await MClient.Entities.SaveAsync(Context.User);
    

## セットアップ

-   *ユーザー登録後*スクリプト[を作成](https://doc.sitecore.com/ch/ja/developers/cloud-dev/work-with-scripts.html "Work with scripts")、公開、有効化します。