---
title: "User post-registration script"
slug: "user-post-registration-script"
abstract: "Web script example to add a user to a specific user group if the external login provider is Google."
---
# User post-registration script

This example shows a *User post-registration* web script that is executed after a new user is added to Sitecore Content Hub. It adds the user to a specific user group if the external provider is Google.

> [!note]
> **Before you begin**
> -   Configure the user groups or profiles required during the pre-registration process.
>
> -   Create a user group called *Google*.
>
> -   Ensure schema properties align with the script logic for pre-registration workflows.

## Script

```csharp
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);

```

## Script explanation

This section steps through the script in execution order, explaining each part. The numbered items describe the sequence, not instructions to perform.

1. Include necessary libraries.

    ```csharp
    using System.Linq;
    ```

2. Check the external user provider. If the provider is not `Google`, the script exits.

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

3. Create a [query](https://doc.sitecore.com/ch/en/developers/cloud-dev/linq-queries.html "LINQ queries") that gets the **Google user group**.

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

4. Execute the query to get the Google group ID.

    ```csharp
    var googleGroupId = await MClient.Querying.SingleIdAsync(query);
    ```

5. If the user group cannot be found, throw an `InvalidOperationException`.

    ```csharp
    if (!googleGroupId.HasValue) throw new InvalidOperationException("Google usergroup not found.");
    ```

6. Get the user's user group relation.

    ```csharp
    var relation = await Context.User.GetRelationAsync<IChildToManyParentsRelation>("UserGroupToUser");
    ```

7. Add the Google group to the user's object via the user group relation.

    ```csharp
    relation.Parents.Add(googleGroupId.Value);
    ```

8. Save the user entity.

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

## Setup

- [Create](https://doc.sitecore.com/ch/en/developers/cloud-dev/work-with-scripts.html "Work with scripts"), publish, and enable the *User post-registration* script.