Add group to directory

Hi Community,

I want to create a group for a specific directory.

Well the group request has no id for a group: Crowd 4.1.0

How can I add a new group to a directory?

BG + Cheers

Hi Thomas,

From Crowd administration, you need first to create an app connected to direcoty of interest: Adding an Application | Crowd Data Center and Server 5.1 | Atlassian Documentation

Then, you’ll need to create basic auth credentials base64 encoded for API call, with your application’s name as $login and application’s password as $password in following powershell code:

function GetBasicAuthCredentials {
    param(
        [Parameter(Mandatory=$True)][Alias("l")][String]$login,
        [Parameter(Mandatory=$True)][Alias("pw")][String]$password
    )
    $AuthString = "{0}:{1}" -f $login, $password
    $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
    return [Convert]::ToBase64String($AuthBytes)
}

Finally, here a sample of pws code to add a group to directory of interest:

# Authentication
$applicationName = "Foo"
$applicationPassword = "Bar"
$appBasicCreds = GetBasicAuthCredentials -l $applicationName -pw $applicationPassword
###

$crowdHost = "http://localhost:8095/crowd"

# Group information
$description = "Blablabla"
$groupname = "my-awesome-group"
###

# Parse data to Json for request body
$data = @{description=[String]$description; name=[String]$groupname; active=$True; type="GROUP"} | ConvertTo-Json -Depth 5

try {
    $response = Invoke-RestMethod -Method POST `
        -Uri "$crowdHost/rest/usermanagement/latest/group" `
        -Headers @{
            "Authorization"="Basic $appBasicCreds"
            "Accept"="application/json"
            "Content-Type"="application/json"
        } `
        -Body $data
    
    Write-Host "Group successfully added to directory." -ForegroundColor Green

} catch {
    Write-Warning $_.Exception.Response.StatusCode.Value__

    if ($_.Exception.Response.StatusCode.Value__ -eq 400) {
        Write-Warning "Group already exists."
    } elseif ($_.Exception.Response.StatusCode.Value__ -eq 403) {
        Write-Warning "The application is not allowed to create a new group."
    } else {
        Write-Warning $_.Exception.Response.StatusDescription
    }
}