Requesting SharePoint User Profiles

A couple weeks ago I had to call SharePoint user profile properties using code. However, it took me quite some searching to make it work properly (but that might just be me?).

Anyway, I thought I'd just share the snippet with the rest of the world, so here you go :)

using System.Web;

using Microsoft.SharePoint;

using Microsoft.Office.Server.UserProfiles;

using Microsoft.Office.Server;

 

namespace Nick.Blog

{

    public class RequestUserProfile

    {

        public UserProfile GetUserProfile()

        {

            using (SPSite site = new SPSite("http://myserver.com"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    ServerContext context = ServerContext.GetContext(web.Site);

                    UserProfileManager upMgr = new UserProfileManager(context);

 

                    UserProfile profile = upMgr.GetUserProfile("accoutName");

 

                    return profile;

                }

            }

        }

    }

}

You'll probably need to run this code in Elevated privileges, as not everyone can call another user's profile, but I guess you get to the point with this one …

Having this profile, you can request all kinds of information from the user, including custom defined properties of the user profile.

Hope it helps someone ;)

Leave a Reply