Groups & Users
Managing groups, requesting users
Site groups and users can be requested via Web's .SiteGroups() and .SiteUsers() queriable collections correspondingly.

Getting users sample

1
users, err := sp.Web().SiteUsers().
2
Select("Email,Id").
3
Filter("Email ne ''").
4
OrderBy("Id", true).
5
Get()
6
7
if err != nil {
8
log.Fatal(err)
9
}
10
11
for _, user := range users.Data() {
12
fmt.Printf("%d: %s\n", user.Data().ID, user.Data().Email)
13
}
Copied!

Getting groups sample

1
groups, err := sp.Web().SiteGroups().Get()
2
if err != nil {
3
log.Fatal(err)
4
}
5
6
for _, group := range groups.Data() {
7
fmt.Printf("%d: %s\n", user.Data().ID)
8
}
Copied!

Ensuring a user

You can't add new user via SharePoint API, but a user who exists in AD/AAD can be added to a site by ensuring him/her by a logon name.
1
user, err := sp.Web().EnsureUser("[email protected]")
2
if err != nil {
3
log.Fatal(err)
4
}
5
6
fmt.Printf("User: %+v\n", user)
Copied!

Associated groups

We love the "power of defaults". Each web by default has three predefined groups: Owners, Members and Visitors. But their IDs and names are different from web to web. Luckily there is a helper for getting associated groups.
1
// .Members() and .Owners() correspondingly
2
group, err := sp.Web().AssociatedGroups().Visitors().Get()
3
if err != nil {
4
log.Fatal(err)
5
}
6
7
fmt.Printf(
8
"Visitors group ID %d, Title \"%s\"\n",
9
group.Data().ID,
10
group.Data().Title,
11
)
Copied!

Creating groups

1
group, err := sp.Web().SiteGroups().Add("My group", nil)
2
if err != nil {
3
log.Fatal(err)
4
}
5
6
fmt.Printf("New group ID: %d\n", group.Data().ID)
Copied!

Deleting groups

1
// or .RemoveByID(groupID)
2
if err := sp.Web().SiteGroups().RemoveByLoginName("My group"); err != nil {
3
log.Fatal(err)
4
}
Copied!

Adding user to a group

1
user, err := sp.Web().EnsureUser("[email protected]")
2
if err != nil {
3
log.Fatal(err)
4
}
5
6
fmt.Printf("User login: %s\n", user.LoginName)
7
// User login: i:0#.f|membership|[email protected]
8
9
visitorGroup := sp.Web().AssociatedGroups().Visitors()
10
if err := visitorGroup.AddUser(user.LoginName); err != nil {
11
log.Fatal(err)
12
}
Copied!
In group's .AddUser method the argument should be full and valid login name including security provider membership prefix. For instance, while you can ensure Jane using [email protected] the same as an .AddUser method will fail as Jane's login is actually different i:0#.f|membership|[email protected].
If you already know UserID but not sure about LoginName .AddUserByID helper is at the disposal.

Removing users from a group

Similarly as with adding users:
1
user, err := sp.Web().EnsureUser("[email protected]")
2
if err != nil {
3
log.Fatal(err)
4
}
5
6
memberGroup := sp.Web().AssociatedGroups().Members()
7
if err := memberGroup.RemoveUser(user.LoginName); err != nil {
8
log.Fatal(err)
9
}
10
11
// or
12
13
if err := memberGroup.RemoveUserByID(user.ID); err != nil {
14
log.Fatal(err)
15
}
Copied!

Managing group owner

1
memberGroup := sp.Web().AssociatedGroups().Members()
2
if err := memberGroup.SetAsOwner(user.ID); err != nil {
3
log.Fatal(err)
4
}
Copied!

Getting group's users

1
users, err := sp.Web().AssociatedGroups().Visitors().
2
Users().Select("Id,Title").Get()
3
4
if err != nil {
5
log.Fatal(err)
6
}
7
8
for _, user := range users.Data() {
9
fmt.Printf("%d: %s\n", user.Data().ID, user.Data().Title)
10
}
Copied!

Summary

That's it, most of the common actions with groups and users are covered.
You'd probably will be interested with the connected topics:
Last modified 1yr ago