WSS – Get User Profile Details Programmatically

WSS – Get User Profile Details Programmatically

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.

SharePoint My Settings

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.

[code lang="csharp"]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”;

}

}[/code]

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! :-)

19 thoughts on “WSS – Get User Profile Details Programmatically

  1. 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!

    1. 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

  2. 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?

    1. 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

  3. 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?

  4. 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 (:

  5. 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

  6. Iyappan

    Hi
    I m new share point and I know how retrieve users list info.
    My doubt is how to retrive the users with their role (Premission Level).

    Can you please help out me. Its urgent.

    Thanks in advance.

  7. Sri

    Hey Alex,

    I am working on Sharepoint Foundation 2010, trying to build a memebership database. Unfortunately, SF2010 does not have built in ways to enable selective editing of the My Settings page (especially since I have created additional feilds there).

    So, it seems like I have to create 2 web parts (I think?) and then add it to the site so that admin level users can edit all fields and regular users can edit whatever they are allowed to.

    Your code on this page seems like it will do the trick, to get all the info and then use ASP.NET to enable selective editing of the data retrieved. What do you think?

    Thanks for the post.
    Sri

    1. Alex

      Hi Sri,

      I am not sure what you are trying to accomplish exactly – do you mean a grid of all the users in the site allowing you to edit all the properties? This code should do it – unless there is updated object model code for 2010.

      Cheers

      Alex

  8. Hi Alex,I updated my Post

    Hi Alex,
    First thing I really appolize for my Post But I tried in my machine for my project and found helpful.I was new to blogging at initial stage.Now I update that Post with your name also.Even I have not deleted your comment as well.So whenever some visiti my post they will see your link and they can see my mistake also.

    Well Done For blogs Alex.

    All the BEST for the Future.

    Ravi

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">