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

> Learn how to use the Memberships Query Hook with the Delegated Administration Extension.

# Delegated Administration: Memberships Query Hook

When creating a new user, the User Interface shows a dropdown where you can choose the membership(s) you want assigned to a user. These memberships are then defined using the **Memberships Query Hook**.

## Hook contract

* **ctx**: Context object.
* **callback(error, \{ createMemberships: true/false, memberships: })**: Callback to which you can return an error and an object containing the membership configuration.

## Sample use

Users of the IT department should be able to create users in other departments. Users from other departments should only be able to create users for their departments.

```javascript lines theme={null}
function(ctx, callback) {
  var currentDepartment = ctx.payload.user.app_metadata && ctx.payload.user.app_metadata.department;
  if (!currentDepartment || !currentDepartment.length) {
    return callback(null, [ ]);
  }

  if (currentDepartment === 'IT') {
    return callback(null, [ 'IT', 'HR', 'Finance', 'Marketing' ]);
  }

  return callback(null, [ ctx.payload.user.app_metadata.department ]);
}
```

## Notes

Because you can only use this query in the UI, you'll need to assign memberships using the **Write Hook** if you need to enforce rules regarding the assignment of users to specific departments.

If there is only one membership group possible, the Memberships field will not show in the UI.

You can allow the end user to enter any value into the **memberships** field by setting **createMemberships** to true:

```javascript lines theme={null}
function(ctx, callback) {
  var currentDepartment = ctx.payload.user.app_metadata.department;
  if (!currentDepartment || !currentDepartment.length) {
    return callback(null, [ ]);
  }

  return callback(null, {
    createMemberships: ctx.payload.user.app_metadata.department === 'IT' ? true : false,
    memberships: [ ctx.payload.user.app_metadata.department ]
  });
}
```

## Learn more

* [Delegated Administration: Access Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-access-hook)
* [Delegated Administration: Filter Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-filter-hook)
* [Delegated Administration: Settings Query Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-settings-query-hook)
* [Delegated Administration: Write Hook](/docs/customize/extensions/delegated-administration-extension/delegated-administration-hooks/delegated-administration-write-hook)
