---
title: Groups
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

## Groups

### Creating a group

```csharp

// Creates a new group object
GroupInfo newGroup = new GroupInfo();

// Sets the properties for the group
newGroup.GroupDisplayName = "New group";
newGroup.GroupName = "NewGroup";
newGroup.GroupSiteID = SiteContext.CurrentSiteID;
newGroup.GroupDescription = "";
newGroup.GroupApproveMembers = GroupApproveMembersEnum.AnyoneCanJoin;
newGroup.GroupAccess = SecurityAccessEnum.AllUsers;
newGroup.GroupApproved = true;
newGroup.GroupApprovedByUserID = MembershipContext.CurrentUserProfile.UserID;
newGroup.GroupCreatedByUserID = MembershipContext.CurrentUserProfile.UserID;
newGroup.AllowCreate = SecurityAccessEnum.GroupMembers;
newGroup.AllowDelete = SecurityAccessEnum.GroupMembers;
newGroup.AllowModify = SecurityAccessEnum.GroupMembers;
newGroup.GroupNodeGUID = Guid.Empty;

// Saves the group to the database
GroupInfoProvider.SetGroupInfo(newGroup);

```

[> Back to list of examples](#groups-toc)

### Updating a group

```csharp

// Gets the group
GroupInfo updateGroup = GroupInfoProvider.GetGroupInfo("NewGroup", SiteContext.CurrentSiteName);
if (updateGroup != null)
{
    // Updates the group properties
    updateGroup.GroupDisplayName = updateGroup.GroupDisplayName.ToLowerCSafe();

    // Saves the changes to the database
    GroupInfoProvider.SetGroupInfo(updateGroup);
}

```

[> Back to list of examples](#groups-toc)

### Updating multiple groups

```csharp

// Prepares a condition for loading all groups whose name starts with 'NewGroup'
string where = "GroupName LIKE N'NewGroup%'";

// Gets the groups based on the condition
InfoDataSet<GroupInfo> groups = GroupInfoProvider.GetGroups(where, null);

// Loops through the group objects
foreach (GroupInfo group in groups)
{
    // Updates the group properties
    group.GroupDisplayName = group.GroupDisplayName.ToUpper();

    // Saves the changes to the database
    GroupInfoProvider.SetGroupInfo(group);
}

```

[> Back to list of examples](#groups-toc)

### Deleting a group

```csharp

// Gets the group
GroupInfo deleteGroup = GroupInfoProvider.GetGroupInfo("NewGroup", SiteContext.CurrentSiteName);

if (deleteGroup != null)
{
    // Deletes the group
    GroupInfoProvider.DeleteGroupInfo(deleteGroup);
}

```

[> Back to list of examples](#groups-toc)

## Group members

### Creating a group member

```csharp

// Gets the group
GroupInfo group = GroupInfoProvider.GetGroupInfo("NewGroup", SiteContext.CurrentSiteName);

if (group != null)
{
    // Gets the user
    UserInfo user = UserInfoProvider.GetUserInfo("Andy");

    if (user != null)
    {
        // Creates a group member object
        GroupMemberInfo newMember = new GroupMemberInfo();

        // Sets the member properties
        newMember.MemberGroupID = group.GroupID;
        newMember.MemberApprovedByUserID = MembershipContext.CurrentUserProfile.UserID;
        newMember.MemberApprovedWhen = DateTime.Now;
        newMember.MemberInvitedByUserID = MembershipContext.CurrentUserProfile.UserID;
        newMember.MemberUserID = user.UserID;
        newMember.MemberJoined = DateTime.Now;
        newMember.MemberComment = "New member added through the API.";

        // Saves the member to the database
        GroupMemberInfoProvider.SetGroupMemberInfo(newMember);
    }
}

```

[> Back to list of examples](#groups-toc)

### Updating a group member

```csharp

// Gets the group 
GroupInfo group = GroupInfoProvider.GetGroupInfo("NewGroup", SiteContext.CurrentSiteName);

if (group != null)
{
    // Gets the user
    UserInfo user = UserInfoProvider.GetUserInfo("Andy");

    if (user != null)
    {
        // Gets the group member matching the user
        GroupMemberInfo updateMember = GroupMemberInfoProvider.GetGroupMemberInfo(user.UserID, group.GroupID);
        if (updateMember != null)
        {
            // Updates the member properties
            updateMember.MemberComment = updateMember.MemberComment.ToLowerCSafe();

            // Saves the changes to the database
            GroupMemberInfoProvider.SetGroupMemberInfo(updateMember);
        }
    }
}

```

[> Back to list of examples](#groups-toc)

### Updating multiple group members

```csharp

// Gets the group 
GroupInfo group = GroupInfoProvider.GetGroupInfo("NewGroup", SiteContext.CurrentSiteName);

if (group != null)
{
    // Prepares a condition for loading all members of the specified group
    string where = "MemberGroupID =" + group.GroupID;

    // Gets the group members based on the condition
    InfoDataSet<GroupMemberInfo> members = GroupMemberInfoProvider.GetGroupMembers(where, null);

    // Loops through individual group members
    foreach (GroupMemberInfo member in members)
    {
        // Updates the member properties
        member.MemberComment = member.MemberComment.ToUpper();

        // Saves the changes to the database
        GroupMemberInfoProvider.SetGroupMemberInfo(member);
    }
}

```

[> Back to list of examples](#groups-toc)

### Deleting a group member

```csharp

// Gets the group 
GroupInfo group = GroupInfoProvider.GetGroupInfo("NewGroup", SiteContext.CurrentSiteName);

if (group != null)
{
    // Gets the user
    UserInfo user = UserInfoProvider.GetUserInfo("Andy");

    if (user != null)
    {
        // Gets the group member matching the user
        GroupMemberInfo deleteMember = GroupMemberInfoProvider.GetGroupMemberInfo(user.UserID, group.GroupID);

        if (deleteMember != null)
        {
            // Deletes the group member
            GroupMemberInfoProvider.DeleteGroupMemberInfo(deleteMember);
        }
    }
}

```

[> Back to list of examples](#groups-toc)

### Creating an invitation to a group

```csharp

// Gets the group 
GroupInfo group = GroupInfoProvider.GetGroupInfo("NewGroup", SiteContext.CurrentSiteName);

if (group != null)
{
    // Creates a group invitation 
    InvitationInfo newInvitation = new InvitationInfo();

    // Sets invitation properties
    newInvitation.InvitationComment = "Group invitation created through the API.";
    newInvitation.InvitationGroupID = group.GroupID;
    newInvitation.InvitationUserEmail = "user@localhost.local";
    newInvitation.InvitedByUserID = MembershipContext.AuthenticatedUser.UserID;
    newInvitation.InvitationCreated = DateTime.Now;
    newInvitation.InvitationValidTo = DateTime.Now.AddDays(1);

    // Saves the invitation to the database
    InvitationInfoProvider.SetInvitationInfo(newInvitation);

    // Sends the invitation email
    InvitationInfoProvider.SendInvitationEmail(newInvitation, SiteContext.CurrentSiteName);
}

```

[> Back to list of examples](#groups-toc)
