> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-cms/17.latest/extend-your-project/server-side-extensions/management/using-services/userservice.md).

# User Service

This will show you how to perform various User management using the Umbraco service layer.

Learn how to use the User Service to manage the users on your Umbraco project.

## Assigning a User to a User Group

To assign a User to a User Group, we need both the `IUserService` and `IUserGroupService`. As with all Umbraco services, these are obtained using dependency injection.

1. Start by defining an interface for our implementation:

{% code title="ISampleUserHandler.cs" %}

```csharp
namespace UmbracoDocs.Samples;

public interface ISampleUserHandler
{
    Task<bool> AssignUserToAdminGroup(string email, Guid performingUserKey);
}
```

{% endcode %}

2. Next we implement the interface. This implementation holds the dependency to the Umbraco services:

{% code title="SampleUserHandler.cs" %}

```csharp
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;

namespace UmbracoDocs.Samples;

public class SampleUserHandler : ISampleUserHandler
{
    private readonly IUserService _userService;
    private readonly IUserGroupService _userGroupService;

    public SampleUserHandler(IUserService userService, IUserGroupService userGroupService)
    {
        _userService = userService;
        _userGroupService = userGroupService;
    }

    public async Task<bool> AssignUserToAdminGroup(string email, Guid performingUserKey)
    {
        IUser? user = _userService.GetByEmail(email);
        if (user is null)
        {
            return false;
        }

        Attempt<UserGroupOperationStatus> result = await _userGroupService.AddUsersToUserGroupAsync(
            new UsersToUserGroupManipulationModel(Constants.Security.AdminGroupKey, [user.Key]),
            performingUserKey
        );

        return result.Success;
    }
}
```

{% endcode %}

3. Register the implementation in a Composer:

{% code title="SampleUserHandlerComposer.cs" %}

```csharp
using Umbraco.Cms.Core.Composing;

namespace UmbracoDocs.Samples;

public class SampleUserHandlerComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
        => builder.Services.AddSingleton<ISampleUserHandler, SampleUserHandler>();
}
```

{% endcode %}

4. Lastly, we need to put our implementation to use. This could be done in a Management API controller:

{% code title="SampleUserController.cs" %}

```csharp
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Controllers;
using Umbraco.Cms.Api.Management.Routing;
using Umbraco.Cms.Core.Security;

namespace UmbracoDocs.Samples;

[ApiExplorerSettings(GroupName = "Sample user handler")]
[VersionedApiBackOfficeRoute("sample/user-handler")]
public class SampleUserController : ManagementApiControllerBase
{
    private readonly ISampleUserHandler _sampleUserHandler;
    private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;

    public SampleUserController(
        ISampleUserHandler sampleUserHandler,
        IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
    {
        _sampleUserHandler = sampleUserHandler;
        _backOfficeSecurityAccessor = backOfficeSecurityAccessor;
    }

    [HttpPut]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> AssignUserToAdminGroup(string email)
        => await _sampleUserHandler.AssignUserToAdminGroup(email, CurrentUserKey(_backOfficeSecurityAccessor))
            ? Ok()
            : BadRequest();
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/umbraco-cms/17.latest/extend-your-project/server-side-extensions/management/using-services/userservice.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
