SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

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 ;)


Posted 01-21-2008 10:37 AM by nsevens

Comments

Alvin wrote re: Requesting SharePoint User Profiles
on 01-21-2008 11:46 AM

I also had the same problem and did something similar ... but I retrieve all profiles stored it ina DataSet and cache it and then perform filters based on the different criteria ... jus wanted to share as well and I did use elevated permission also....

public void GetUserProfile()

       {

           #region Local variables

           SPSite site = SPContext.Current.Site;

           SPWeb web = SPControl.GetContextWeb(Context);

           #endregion

           SPSecurity.RunWithElevatedPrivileges(delegate()

           {

               using(SPSite ElevatedSiteCollection = new SPSite(site.ID))

               {

                   using(SPWeb ElevatedSite = ElevatedSiteCollection.OpenWeb(web.ID))

                   {

                       ServerContext context = ServerContext.GetContext(ElevatedSiteCollection);

                       UserProfileManager profileManager = new UserProfileManager(context);

                       BindDataView(new DataView(), false);

                       DataSet ds = CreateDataSet( profileManager );

                       ds = UpdateManagerInfo(ds, "Manager", "Account name", "Manager e-mail");

                       Context.Cache["dsEmployee"] = ds;

                       if(this._searchstring.Length > 0 && !this._searchstring.Equals("*"))

                           this._filterexp += " AND Name Like '%" + _searchstring.Replace("'", "''") + "%' ";

                       DataView dv = new DataView(ds.Tables["Employees"], _filterexp, _sortexp, DataViewRowState.CurrentRows);

                       BindDataView(dv, false);

                   }

               }

           });

       }

FrostSpark wrote re: Requesting SharePoint User Profiles
on 09-19-2008 9:08 AM

Hi!

I Would like to make a web part, that can droaw a map for every user, to show where he/she sit inside a building.

My problem was, that i couldn't  found, how can i identify the user, who has the user's profile page. I read the codes on this site, but I coud not know where i can use these codes.

I need a working web part on the my profiles page, which can identify the user, who has that profile site (so not the login user!), and maybe put his/her name in a list (for example), or read from a list the same name, where the name is a varriable in a list or something like that, becouse I have a lot of web part, whiches works, and deals with lists for that project. Any Idea how I can make this task?

Any help would be welcomed.

Thanx, yours:

FrostSpark

nsevens wrote re: Requesting SharePoint User Profiles
on 10-10-2008 2:42 AM

Frostspark,

I may be understanding this wrong, but it seems to me you just want to retrieve a user's profile. This can easily be done by the UserProfile profile = upMgr.GetUserProfile("accoutName");

You can specify your own accountname (the one from the user you want to request i suppose) and access it's properties as needed (provided they are visible for everyone etc.)

J wrote re: Requesting SharePoint User Profiles
on 11-08-2008 1:52 AM

Hi

Using your code I tried to do the following:

UserProfile profile = GetUserProfile();

label.Text = profile.ID.ToString();

I'm trying to display the profile id in the webpart Id

However, it throws the following error when i tried to open my sharepoint site:

An unexpected error has occurred.

Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator.

Troubleshoot issues with Windows SharePoint Services.

------------------------------

did I do anything wrong ? Thanks in advance !

nsevens wrote re: Requesting SharePoint User Profiles
on 11-09-2008 3:56 AM

J,

Try putting your web application in debug mode, so you can view the real error when opening the page.

If you don't know how to do this, it's explained here for example:

blog.thekid.me.uk/.../a-solution-to-quot-an-unexpected-error-has-occurred-quot-in-wss-v3.aspx

sandeep wrote re: Requesting SharePoint User Profiles
on 03-17-2009 10:56 PM

one thing i noticed in ur code is

u got SPSite , then u got SPWeb from it and from SPWeb u again got site ( web.Site ) ????

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.