A SharePoint 2007 (MOSS/WSS) WebPart showing the list of users of a site with a link to corresponding MySite

Add a reference of Microsoft.Office.Server.dll. Here is the Code:

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

using Microsoft.Office.Server.Administration;

using Microsoft.Office.Server.UserProfiles;

using Microsoft.Office.Server;

using System.IO;

namespace UserProfileWP

{

    [Guid("e36b58a7-fbe9-4ecf-b545-831929545ebf")]

    public class UserProfileWP : System.Web.UI.WebControls.WebParts.WebPart

    {

        string output = "<table border=\"0\">";

        protected override void Render(HtmlTextWriter writer)

        {

            // TODO: add custom rendering code here.

            EnsureChildControls();

     writer.Write(output);

        }

        protected override void CreateChildControls()

        {

            try

            {

                using (SPSite site = new SPSite("<your site url>"))

                {

                    ServerContext context =

                        ServerContext.GetContext(site);

                    UserProfileManager profileManager = new

                        UserProfileManager(context);

                    foreach (UserProfile profile in profileManager)

        {

                        //Console.WriteLine(profile[PropertyConstants.AccountName]);

                        output += "<tr><td><a href=\"" + profile.PublicUrl.AbsoluteUri + "\"/>" + profile[PropertyConstants.AccountName] + "</a></td></tr>";

  }

                    output += "</table>";

                }

            }

            catch (Exception ex)

            {

                output += "<tr><td>" + ex.ToString() + "</td></tr></table>";

            }

        }

    }

}

 

Addition: While using this Web Part you may find errors saying "Access Denied: Only an administrator may enumerate through all user profiles" for users other than Administrator. If you want to remove this, you have to set the User Permission using Shared Services. A nice post related to this:

 

https://edinkapic.blogspot.com/2007/08/enumerating-user-profiles.html

Comments

  • Anonymous
    September 16, 2007
    PingBack from http://stevepietrekweblog.wordpress.com/2007/09/16/links-9162007/

  • Anonymous
    September 16, 2007
    Schritt-für-Schritt zur eigenen Web Content Management Site und diese dann mit optimierter Performance

  • Anonymous
    October 12, 2007
    Pranab,  Can i filter subset of users using UserProfileManager ?  I want to get a list of all the users based on their location instead of looping through all the profiles. Is there any way to apply SPQuery ? -Thanks

  • Anonymous
    December 14, 2007
    Hi, Sharepoint 2007 is very interesting to discover, Well I'm pretty new with sharepoint, but I do have the desire to learn and create different things with sharepoint.I have couple of questions and I need help first of all how can I create a web Part for Organizational Hierarchy list, with out any links to Mysite, mean I can add a webpart to any page I want. Secondly how can I build a webpart or any webpart out there that allow me to do just like wikipedia? thank you very much for your help

  • Anonymous
    March 09, 2008
    Accessing the User Profile Store Using the Object Model: http://msdn2.microsoft.com/en-us/library/ms574336

  • Anonymous
    September 15, 2008
    Hi Pranab, I am new to sharepoint and I have few questions as follows:

  1. How do i create a web Part for Organizational Hierarchy list in WSS 3.0 specially, as SSP and Profile management does not exists in WSS 3.0
  2. what are the pros & cons of 'User Information List' when used. Thanks, Vishnu Rana India
  • Anonymous
    September 16, 2008
    Hi Vishnu, I am also looking for these answers. If i get something, i will reply. Thanks, Nitin Gupta

  • Anonymous
    October 14, 2008
    Hello this code doesn't work on my vs. can you upload a ready webpart? i need it very much thanx a lot

  • Anonymous
    May 15, 2009
    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