Search code examples
c#azuremicrosoft-graph-apimicrosoft-teams

Using MS Graph Api to create Group matching suffix requirements


UPDATE / Conclusion

Seems like my problem was that we use '&' and '/' in our organization names, which looks to be the problem. Creating of groups which follows the suffix rules, with department name matching [a-Z] will work.

ORIGINAL QUESTION:

I'm trying to create groups and assigning owners/users, but I keep getting this error:

The property is missing a required prefix/suffix per your organization's Group naming requirements.

Our organization got a rule that every group name requires a suffix of type "Department".

This is my new group class:

var newGroup = new Group
{
    DisplayName = $"{displayName}-{userDepartment}",
    Description = description,
    GroupTypes = new List<string> { "Unified" },
    MailEnabled = false,
    MailNickname = mailNickName,
    SecurityEnabled = true,
    AdditionalData = new Dictionary<string, object>()
    {
        {
            "owners@odata.bind", userIds.Select(x => $"https://graph.microsoft.com/v1.0/users/{x}").ToList()
        }
    }
};

What am I missing to be compliant with the requirements?

EDIT Added image: enter image description here


Solution

  • I created one group naming policy same as you in my tenant like this:

    enter image description here

    Initially, I too got same error when I ran your code in my environment as below:

    enter image description here

    Note that, group naming policies are applicable for both display name and alias (mailNickName) of Microsoft 365 groups.

    In my case, I used below modified code by adding suffix to both displayName & mailNickName and got the response like this:

    using Microsoft.Graph;
    using Azure.Identity;
    using Microsoft.Graph.Models;
    
    namespace GroupCreationApp
    {
        class Program
        {
            private static string[] scopes = new[] { "Group.ReadWrite.All" };
    
            static async Task Main(string[] args)
            {
                var displayName = "Library Assist";
                var userDepartment = "IT";
                var description = "Self help community for library";
                var mailNickName = $"library-{userDepartment}";
                var ownerIds = new List<string>
                {
                    "userid01",
                    "userid02"
                };
    
                var groupDisplayName = $"{displayName}-{userDepartment}";
    
                var graphClient = GetGraphClient();
    
                await CreateGroup(graphClient, groupDisplayName, description, mailNickName, ownerIds);
    
                Console.WriteLine("Group creation completed.");
            }
    
            private static GraphServiceClient GetGraphClient()
            {
                var tenantId = "tenantId";
                var clientId = "appId";
    
                var options = new InteractiveBrowserCredentialOptions
                {
                    TenantId = tenantId,
                    ClientId = clientId,
                    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
                    RedirectUri = new Uri("http://localhost")
                };
    
                var interactiveCredential = new InteractiveBrowserCredential(options);
    
                var graphClient = new GraphServiceClient(interactiveCredential, scopes);
    
                return graphClient;
            }
    
            private static async Task CreateGroup(GraphServiceClient graphClient, string displayName, string description, string mailNickName, List<string> ownerIds)
            {
                try
                {
                    var newGroup = new Group
                    {
                        DisplayName = displayName,
                        Description = description,
                        GroupTypes = new List<string> { "Unified" },
                        MailEnabled = true,
                        MailNickname = mailNickName,
                        SecurityEnabled = false,
                        AdditionalData = new Dictionary<string, object>
                        {
                            { "owners@odata.bind", ownerIds.Select(id => $"https://graph.microsoft.com/v1.0/users/{id}").ToList() }
                        }
                    };
    
                    var result = await graphClient.Groups.PostAsync(newGroup);
    
                    Console.WriteLine($"Group '{result.DisplayName}' created successfully with ID: {result.Id}");
                }
                catch (ServiceException ex)
                {
                    Console.WriteLine($"Error creating group: {ex.Message}");
                }
            }
        }
    }
    

    Response:

    enter image description here

    To confirm that, I checked the same in Portal where new Microsoft 365 group created successfully with below properties:

    enter image description here