A Place For Answers

Posts tagged “User Profiles

SharePoint 2010: User Profiles Connections

In SharePoint 2010 the social aspect took a step up in the right direction, it allowed users to follow other users much like Twitter and subscribe to what those users do.

You can access that piece of data regarding the user profile using the “Microsoft.Office.Server.UserProfiles” dll, like so:

using (SPSite site = new SPSite("Site URL"))
    SPServiceContext ctx = SPServiceContext.GetContext(site);
    UserProfileManager upm = new UserProfileManager(ctx);
    UserProfile profile = upm.GetUserProfile("Domain\\UserName");
    // profile holds all the data about the given user

What’s cool about this is the ability to connect people, say Jonathan is following John that’s following Joe.
Now SharePoint 2010 handles the following part pretty nicely and you can find each user, that your user is following like finding out that John is following Joe using:

Colleague[] colleagues = profile.Colleagues.GetItems();

The Colleague class connects a user profile found under Colleague.Profile to the user profile that’s following it, more information can be found here.

But! what if you wanted to go the other way around and find all the user profiles following a specific user?
Well here’s where SharePoint 2010 gets lost, btw SharePoint 2013 does give you a simpler way to grab this data, but here’s where digging around in msdn goes a long way…

You see when a user subscribers to another user profile’s feed, an event occurs, that event can then be scraped from the activity feed using the “Microsoft.Office.Server.ActivityFeed” dll.

ActivityManager am = new ActivityManager(profile);
List<long> bulkRecordIds = new List<long>();
Dictionary<long, MinimalPerson> owners = new Dictionary<long, MinimalPerson>();
Dictionary<long, List<MinimalPerson>> colleaguesOfOwners = new Dictionary<long, List<MinimalPerson>>();

bulkRecordIds.Add(profile.RecordId); // this is only for one user but can easily be translated to multiple users

ActivityFeedGatherer.GetUsersColleaguesAndRights(am, bulkRecordIds, out owners, out colleaguesOfOwners);

At this point, you’ll receive in “colleaguesOfOwners.Keys” a list containing of “MinimalPerson” representation of every user following your man(which you can then translate to a user profile), this way John can find out he’s being followed by Jonathan.

Hope this helps, stay tuned for more posts.