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:

 

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