User Profiles
Dealing with user profiles API
Working with UPS API is simple. UPS API is not something sophisticated as Search for example and it has just a few methods which can be in demand in a server-side operation.
Don't get me wrong, there are not-covered social features from within user profiles API in Gosip but we rarely have seen demand and scenarios of their usage in SharePoint solutions. Anyways, if some additional feature coverage is required for you please let us know by posting an issue.

Getting profiles

The most in-demand, I would say, feature when it comes to user profiles is getting all profiles. However, UPS API allows only dealing with a single profile, you can't request all of them.
Luckily, this is possible and recommended achieving with the search.
1
res, err := sp.Search().PostQuery(&api.SearchQuery{
2
QueryText: "*",
3
SourceID: "b09a7990-05ea-4af9-81ef-edfab16c4e31",
4
})
5
6
if err != nil {
7
log.Fatal(err)
8
}
9
10
fmt.Printf("%+v\n", res.Results())
Copied!
See a bit more advanced sample.

Getting profile properties

1
user, err := sp.Web().SiteUsers().
2
GetByEmail("[email protected]").Get()
3
4
if err != nil {
5
log.Fatal(err)
6
}
7
8
props, err := sp.Profiles().GetPropertiesFor(user.Data().LoginName)
9
if err != nil {
10
log.Fatal(err)
11
}
12
13
fmt.Printf("%+v\n", props.Data())
Copied!
Gosip strongly types properties response using .Data() helper, here is the resulted struct:
1
type ProfilePropsInto struct {
2
AccountName string
3
DirectReports []string
4
DisplayName string
5
Email string
6
ExtendedManagers []string
7
ExtendedReports []string
8
Peers []string
9
IsFollowed bool
10
PersonalSiteHostURL string
11
PersonalURL string
12
PictureURL string
13
Title string
14
UserURL string
15
UserProfileProperties []*TypedKeyValue
16
}
Copied!

Getting single property

Sometimes you have to be as effective as possible and trim down responses to a minimum. Let's say you only need a single property.
1
rop, err := sp.Profiles().
2
GetUserProfilePropertyFor(user.Data().LoginName, "AccountName")
3
4
if err != nil {
5
log.Fatal(err)
6
}
7
8
fmt.Printf("%s\n", prop)
Copied!
Seriously, I don't know the reason for existing of getting a single property, but not getting specific properties or multiple profiles or updating multiple properties. There are no excuses yet this part of SharePoint API is clunky and really old.

Setting user profile property values

There are two methods for setting user profile property value. Yeah, you heard me the right property in a time.

Set single value profile property

1
if err := sp.Profiles().SetSingleValueProfileProperty(
2
user.Data().LoginName,
3
"AboutMe",
4
"Updated from Gosip",
5
); err != nil {
6
log.Fatal(err)
7
}
Copied!

Set multi valued profile property

1
tags := []string{"#ci", "#demo", "#test"}
2
if err := sp.Profiles().SetMultiValuedProfileProperty(
3
user.Data().LoginName,
4
"SPS-HashTags",
5
tags,
6
); err != nil {
7
log.Fatal(err)
8
}
Copied!

Summary

User profiles API is limited due to its legacy nature. It is what it is. However, many SharePoint solutions, especially intranet portals and workflow processes can be heavily based on UPS. Go worker can be handy for custom synchronization scenarios and also in external workflow workers with UPS as a source for settings for detecting user dynamic roles.
Last modified 2yr ago