Retrieve SharePoint 2010 user social metadata programatically

About a week ago Pete Dutchinson dropped me a note about a cool extension he’s working on, which prompted me to spend some time looking into managing User Profiles programatically; which is what I’ll write about here today.

If you don’t have User Profile service application installed in your environment – just navigate to http://[central admin]/_admin/ServiceApplications.aspx  and get one set up.

Now, once you set it up, navigate to the same Service Application list URL, and select your User Profile Application you will see all sorts of options to work with your user profiles:

user profile service app

User Profiles in SharePoint 2010 hold user Social information and many other properties. You can view those or add new ones through Manage User Properties. If you choose to create a new user profile using Manage User Profiles you will see a set of those properties as well as their scope.

I will focus on few cool properties: Ask Me About, Skills, Interests

Those above are among just few that provide users to picl one of the existing SharePoint 2010 Terms or Tags as the choice. As you can see you can either pick an existing one or create a new one:

properties options

Now, how do you go about assigning one of those programatically or getting a list of existing one, so you can use it in your own webparts or control.

Here is how I get all of the Skills of the currently loggen in user:

//
using Microsoft.Office.Server.UserProfiles;
//

public List<string> GetSkills()
        {
            string socialDataStatsSite = “http://myserver/“;
            List<string> myskills = null;
            using (SPSite siteColl = new SPSite(socialDataStatsSite))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);

                UserProfile userProfile = ProfileLoader.GetProfileLoader().GetUserProfile();
                if (userProfile[PropertyConstants.Skills] != null)
                {
                    myskills = new List<string>();
                    Term[] skillTerms = userProfile[PropertyConstants.Skills].GetTaxonomyTerms();
                    if (skillTerms != null)
                    {
                        foreach (Term term in skillTerms)
                        {
                            if (term != null)
                            {
                                myskills.Add(term.Name);
                            }
                        }
                    }
                }
            }
            return myskills;
        }

Enjoy!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , . Bookmark the permalink.

Comments are closed.