WSS – Get User Profile Details Programmatically

6 04 2009

In WSS, there is a basic list called the ‘User Information List’ which holds some profile details about the users themselves. These details can be viewed/edited from the ‘My Settings’ screen.

ms

WSS holds the following user details:

  • Account (Login Name)
  • Name (Display Name)
  • E-Mail
  • About Me
  • Picture
  • Department
  • Job Title
  • SIP Address

You can get a reference to any of these properties in code simply by querying the list with the ID of the required SPUser:

e.g.

using (SPSite Site = new SPSite(“Site URL”))

{
  using (SPWeb Web = Site.OpenWeb())
{

SPList UserInfoList = Web.Lists[“User Information List”];

SPUser User = Web.Users.GetByID(1); – Put the User ID Here

SPListItem UserItem = UserInfoList.GetItemById(User.ID);

UserItem[“Department”] = “Some Department”;

}

}

Using this list you can set any of the values as required or simply retrieve the stored values. I used the above to get a reference to the ‘Picture’ which is stored as a URL.

Happy Coding! 🙂

Advertisements

Actions

Information

15 responses

11 05 2009
stenloves

Hi, any idea how can i retrieve all the groups that is in the sharepoint site, and from the group knowing which users belongs to that group? Look forward to your reply. Thanks!

11 05 2009
alex350r

Hi, to retrieve all the groups in a SharePoint site – use the following object model code:

SPGroupCollection groupCol = web.groups;

(This will get you a collection of groups in the SharePoint Site)

To get the users of a group (From the group name) –

SPGroup group = web.Groups[“Your Group Name”];

SPUserCollection userCol = group.Users;

You can iterate through the above collections with a foreach and then execute your code for each user/group inside.

Hope it helps!

Alex

11 05 2009
stenloves

Thanks for the reply Alex! Anyway im new to sharepoint and C# so could u pls guide me?

this is what I have now :

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication3
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SPSite site = new SPSite(“http://w2003eer2/”))
{
using (SPWeb oWebsite = site.OpenWeb())
{
foreach (SPGroup Group in web.Groups)
{
SPGroupCollection groupCol = web.groups;
}
foreach(SPUser singleUser in Group.Users)
{
SPGroup group = web.Groups[“Your Group Name”];
SPUserCollection userCol = group.Users
}
}
}

Is this correct?

11 05 2009
alex350r

Errm its sort of correct – what are you executing this code in? a console application? webpage? webpart?

If you let me know what you are using to run the code then I can post the code you need – I am assuming from your earlier comment you would like to list all groups in the site and all users in each of the groups.

Thanks,

Alex

11 05 2009
stenloves

Hi Alex,
Thanks! Yes it will be a web page. It will have 3 buttons.
the 1st button (Retrieve Groups) when click, it will retrieve all the groups that is in the sharepoint site. It will display the names of all the groups in a listbox.
Then when you click onto the group name in the list box and click the 2nd button (Retrieve User) , it will retrieve all the users of that particular group in another listbox.

Able to get what I’m trying to convey?

11 05 2009
alex350r

Ok I see what you are trying to do, do you have a visual studio project at the moment? if so please send it to me – alex.clark@sews-e.com and ill have a look for you. sounds pretty straightforward.

11 05 2009
stenloves

Hi Alex, I’ve sent it to your mail, hope you have received it alr?

11 05 2009
alex350r

Hi, Yes ive got your email – I will take a look at the code as soon as poss and let you know.

Cheers,

Alex

11 05 2009
stenloves

Hi Alex, thanks for your time. The codes I have is pretty much the same of what you’ve guided me earlier. Look forward to your reply anyhows (:

12 05 2009
stenloves

Hi Alex, how’s the code I’ve sent?

15 05 2009
Saurabh Kumar Singh

Hi,
I am just associating a custom Event handler feature with SharePoint “User

Information” list, for creating a copy of newly added user in another sharepoint

custom list with the help of “ItemAdded” event handler, which will maintain user

profile but it doesn’t seems to fire ItemAdding event with “User Information” list.
I have read this article: “http://msdn.microsoft.com/en-us/library/aa979520.aspx”.
So, can you please suggest me that how can i handle events for “User Information”

list?

Thank you,
Saurabh Kumar Singh

9 06 2009
Steve

I’m in the same boat Saurabh. It seems like events are suppressed on User Information List.

30 11 2009
star

Hi,

Any idea how I can get user rss feed subscriptions from their profile?

13 10 2010
Matthew Hughes

Alex,

I stumbled upon this after having an idea whilst at my training and it worked. I managed to create a visual web part and deploy it using your code. So thanks and I am now chuffed to bits woop.
Matt

13 10 2010
alex350r

Cool nice one Matt – glad to hear it helped.

Hope the training is going well 😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: