> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-llms-scoped-indexes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Create A Group

> Create A Group — CometChat documentation.

## Create a Group

*In other words, as a logged-in user, how do I create a public, private or password-protected group?*

You can create a group using `createGroup()` method. This method takes a Group object as a parameter that takes all the information related to the group. So, in order to create a group, you will have to create an object of the group and assign all the values to the group.

To create a group, you can use either of the below two initializers:

1. `new Group(String GUID, String name, String groupType, String password)`
2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)`

The `groupType` needs to be either of the below 3 values:

1.`CometChatConstants.GROUP_TYPE_PUBLIC` (public) 2.`CometChatConstants.GROUP_TYPE_PASSWORD` (password) 3.`CometChatConstants.GROUP_TYPE_PRIVATE` (private)

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    1 -* let GroupTobeCreated = Group(GUID: String, name: String, groupType: CometChatConstants.groupType, password: String?)

    2 -* let GroupTobeCreated = Group(GUID: String, name: String, groupType: CometChatConstants.groupType, password: String?, icon: String, description: String)
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    1 -*  Group *GroupTobeCreated = [[Group alloc]initWithGuid:(NSString * _Nonnull) name:(NSString * _Nonnull) groupType:(enum groupType) password:(NSString * _Nullable)];
        
    2 -*  Group *GroupTobeCreated = [[Group alloc]initWithGuid:(NSString * _Nonnull) name:(NSString * _Nonnull) groupType:(enum groupType) password:(NSString * _Nullable) icon:(NSString * _Nonnull) description:(NSString * _Nonnull)];
    ```
  </Tab>
</Tabs>

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let guid = "cometchat-guid-11";
    let groupName = "TestGroup1";
    let password = ""; //mandatory in case of password protected group type

    let group = Group(guid: guid, name: groupName, groupType: .private, password: password);

    CometChat.createGroup(group: group, onSuccess: { (group) in

      print("Group created successfully. " + group.stringValue())

    }) { (error) in

      print("Group creation failed with error:" + error!.errorDescription);
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSString *guid = @"cometchat-guid-101";
    NSString *name = @"TestGroup1";
    NSString *password = nil ; // mandatory in case of password protected group type

    Group *group = [[Group alloc]initWithGuid:guid name:name groupType:groupTypePublic password:password];

    [CometChat createGroupWithGroup:group onSuccess:^(Group * group) {
        
        NSLog(@"Group created successfully. %@",[group stringValue]);
        
    } onError:^(CometChatException * error) {
        
        NSLog(@"Group creation failed with error: %@",[error errorDescription]);
    }];
    ```
  </Tab>
</Tabs>

The `createGroup()` method takes the following parameters:

| Parameter | Description |
| - | - |
| group | An instance of `Group` class |

After the successful creation of the group, you will receive an instance of `Group` class which contains all the information about the particular group.

<Warning>
  GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.
</Warning>

## Add members while creating a group

You can create a group and add members at the same time using the `createGroupWithMembers()` method. This method takes the `Group` Object, Array of `Group Member` Object to be added & Array of `UIDs` to be banned.

To create an object of `Group` class, you can use either of the below two constructors:

1. `new Group(String GUID, String name, String groupType, String password)`
2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)`

The `groupType` needs to be either of the below 3 values:

1. `CometChat.GROUP_TYPE.PUBLIC`
2. `CometChat.GROUP_TYPE.PASSWORD`
3. `CometChat.GROUP_TYPE.PRIVATE`

To create an object of `Group Member` class, you can use the below constructor:

* new CometChat.GroupMember(String UID, String scope)

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let group = Group(guid: "cometchat-uid-group1" , name: "Hello Group", groupType: .public, password: nil)
    let members = [GroupMember(UID: "cometchat-uid-4", groupMemberScope: .participant)]
    let banMembers = ["cometchat-uid-2"]
            
    CometChat.createGroupWithMembers(group: group, members: members, banMembers: banMembers, onSuccess: {
    	dict in
    		print("Group created successfully",dict)  
    	}, onError: { (error) in
        print("Some error occured while creating group \(String(describing: error?.errorDescription))")
      }
    )
    ```
  </Tab>
</Tabs>

## Group Class

| Field | Editable | Information |
| - | - | - |
| guid | Needs to be specified at group creation. Cannot be edited later | A unique identifier for a group |
| name | Yes | Name of the group |
| type | No | Type of the group: Can be 1. Public 2. Password 3. Private |
| password | No | Password for the group in case the group is of type password. |
| icon | Yes | An URL to group icon |
| description | Yes | Description about the group |
| owner | Yes | UID of the owner of the group. |
| metadata | Yes | Additional data for the group as Dictionary |
| createdAt | No | The Unix timestamp of the time the group was created |
| updatedAt | No | The Unix timestamp of the time the group was last updated |
| hasJoined | No | A boolean to determine if the logged-in user is a member of the group. |
| joinedAt | No | The Unix timestamp of the time the logged-in user joined the group. |
| scope | Yes | Scope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant |
| membersCount | No | The number of members in the groups |
| tags | Yes | A list of tags to identify specific groups. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.