Thomas
Thomas

Reputation: 41

CSOM set "Make Default Group"

In CSOM (C#) I am creating a subsite and creating SharePoint groups for Owners, Members and Vistors.

The Vistors should be the Default group when sharing. In the UI you can set the Vistors Group to be default by selecting “Make Default Group” under the groups settings.

But how do you do that in CSOM?

Here is my current code for creating a group

    private void SetSecurity(Web web)
    {
        CreateGroup($"Besøgende på {web.Title}", $"Denne gruppe giver læserettighed til {web.Title} sitet", AssociatedGroup.Vistors, web);
        CreateGroup($"Medlemmer af {web.Title}", $"Denne gruppe giver skriverettighed til {web.Title} sitet", AssociatedGroup.Members, web);
        CreateGroup($"Ejere af {web.Title}", $"Denne gruppe giver fuld kontrol til {web.Title} sitet", AssociatedGroup.Owners, web);

        SpClientContext.ExecuteQuery();
    }

    private enum AssociatedGroup
    {
        Owners,
        Members,
        Vistors
    }

    private void CreateGroup(string title, string description, AssociatedGroup associatedGroup, Web web)
    {
        var groupCreation = new GroupCreationInformation();
        groupCreation.Title = title;
        groupCreation.Description = title;

        var group = web.SiteGroups.Add(groupCreation);

        var collRoleDefinitionBinding = new RoleDefinitionBindingCollection(SpClientContext);

        switch (associatedGroup)
        {
            case AssociatedGroup.Owners:
                web.AssociatedOwnerGroup = group;
                web.AssociatedOwnerGroup.Update();
                var adminRoleDefinition = web.RoleDefinitions.GetByType(RoleType.Administrator);
                collRoleDefinitionBinding.Add(adminRoleDefinition);
                break;

            case AssociatedGroup.Members:
                web.AssociatedMemberGroup = group;
                web.AssociatedMemberGroup.Update();
                var contributerDefinition = web.RoleDefinitions.GetByType(RoleType.Contributor);
                collRoleDefinitionBinding.Add(contributerDefinition);
                break;

            case AssociatedGroup.Vistors:
                web.AssociatedVisitorGroup = group;
                web.AssociatedVisitorGroup.Update();
                var vistorDefinition = web.RoleDefinitions.GetByType(RoleType.Reader);
                collRoleDefinitionBinding.Add(vistorDefinition);
                break;
        }

        web.RoleAssignments.Add(group, collRoleDefinitionBinding);

        group.Update();

        web.Update();
    }

I have tried setting the visitors group as the AssociatedMemberGroup but that don’t give the right result, since in the UI when sharing it will be mapped to the Can Edit option.

                case AssociatedGroup.Vistors:
                //web.AssociatedVisitorGroup = group;
                web.AssociatedMemberGroup = group;
                web.AssociatedMemberGroup.Update();
                var vistorDefinition = web.RoleDefinitions.GetByType(RoleType.Reader);
                collRoleDefinitionBinding.Add(vistorDefinition);
                break;

https://i.sstatic.net/J7Ljt.png

So any suggestion on how in CSOM I can mark the Visitors Group as the default group when sharing?

Upvotes: 1

Views: 1685

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

The following example demonstrates how to set default group using CSOM API:

public static void SetDefaultGroup(Group group)
{
     var ctx = group.Context as ClientContext;
     ctx.Web.AssociatedMemberGroup = group;
     ctx.Web.Update();
     ctx.ExecuteQuery();
}

Usage

var group = ctx.Web.SiteGroups.GetById(groupId);
SetDefaultGroup(group);

Upvotes: 4

Related Questions