Luciano Valinho
Luciano Valinho

Reputation: 59

Associate members to group using SenseNet.Client on Sensenet

I am having issues defining the members of a group on sensenet using the .NET API SenseNet.Client.

I need to create an automatic process to add users and groups on sensenet. I know how to create the users and groups, but I didn't find any information about adding users to group.

Here's the code that I use to create a group:

var group = Content.CreateNew("/Root/IMS/BuiltIn/OUtest", "Group", "testGroup");
    group["Name"] = "testGroup";
    group["DisplayName"] = "testGroup";
    await group.SaveAsync();

Upvotes: 0

Views: 48

Answers (1)

Miklós Tóth
Miklós Tóth

Reputation: 1511

To support this scenario, there is a Group class in the Client API that contains a couple of methods for modifying group membership. It inherits from the main Content class, so it has all its features.

If you already have a group id, you may choose the static API for modifying membership (the idArray below should contain new members only, you dot have to know existing members, this is only the 'delta').

// add new members to a group
await Group.AddMembersAsync(group.Id, idArray);

...or the instance API, if you are creating a new Group (note the generic creator method):

// create group using the generic method
var group = Content.CreateNew<Group>("/Root/IMS/BuiltIn/OUtest", "Group", "testGroup");
group["Name"] = "testGroup";
group["DisplayName"] = "testGroup";
await group.SaveAsync();

// add new members
await group.AddMembersAsync(idArray);

// remove members
await group.RemoveMembersAsync(deletedUsersArray);

The methods above make the REST calls immediately, so there is no need to call Save after them.

Upvotes: 1

Related Questions