1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Displaying List & Profiles of Users in a Role

Displaying List & Profiles of Users in a Role

8 posts, 1 answered
  1. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    13 Aug 2009
    Link to this post
    Hello,

    I am developing a small, semi-social-networking site, and one of the functionalities that the client requires is that there be a page which lists all members of a specific role, and each user's name be a link to their individual profile page.

    What would be the best method to implement this?

    Thank you for your support,

    David
  2. John
    John avatar
    32 posts
    Registered:
    08 Oct 2008
    13 Aug 2009
    Link to this post
    Hi Dave,

    Sitefinity is using the same Roles provider from the .NET Framework, so I'd recommend the Roles.GetUsersInRole method. Because that method returns an array of strings, I'd also recommend creating a hyperlink for each user in the role. Here is some sample code:

    ASPX

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateUser.aspx.cs" Inherits="Files_CreateUser" %> 
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml"
    <head runat="server"
        <title></title
        <script type="text/javascript"
         
          function pageLoad() { 
          } 
         
        </script> 
    </head> 
    <body> 
        <form id="form1" runat="server"
        <div> 
            <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
            <asp:Panel ID="Panel1" runat="server" GroupingText="Roles"
            </asp:Panel> 
     
        </div> 
        </form> 
    </body> 
    </html> 
     


    C#

    using System; 
    using System.Web.UI.WebControls; 
    using System.Web.Security; 
     
    public partial class Files_CreateUser : System.Web.UI.Page 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            foreach (string s in Roles.GetUsersInRole("administrators")) 
            { 
                HyperLink link = new HyperLink(); 
                link.NavigateUrl = "http://www.mysite.com/mypage.aspx?UserName=" + s; 
                link.Text = "Page for: " + s; 
                Literal literal = new Literal(); 
                literal.Text = "<p></p>"
                Panel1.Controls.Add(link); 
                Panel1.Controls.Add(literal); 
            } 
        } 
     
     

    -John
  3. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    15 Aug 2009
    Link to this post
    Hi John,

    I suggest using of user control(System.Web.UI.UserControll instead of inheriting from System.Web.UI.Page.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  4. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    17 Aug 2009
    Link to this post
    Thank you for your responses.

    Ivan, would you mind explaining in a little more detail. I was hoping to be able to integrate it nicely with the Sitefinity API. I have created additional fields for the profile, such as a short bio, and I would like those to appear as well.

    I managed to create a page which lists all users by stripping out the code I needed from /Sitefinity/Admin/CmsAdmin/Users.aspx, but I don't think this is optimal, and it also shows all users, not just those in a specific role. I've been working on something very similar to what you suggested, John, using GetUsersInRole("rolename"), but I need more than just the username. I then used GetUserseInRole and passed each username through the GetUser(username) method, which returns an instance of a System.Web.Security.MembershipUser class. This gets me closer to where I want to be, but I don't know where to go to get the user details from sitefinity, i.e. First Name, Last Name, Position, etc.

    Thank you for your support,

    David
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    17 Aug 2009
    Link to this post
    Hello David,

    To get profile data you need to create a new instance of ProfileBase class and use GetPropertyValue method.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
    Answered
  6. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    17 Aug 2009
    Link to this post
    Absolutely fantastic! You folks at Telerik are always a pleasure to work with, because you're courteous, you're timely, and you really know your stuff!

    I'm eagerly looking forward to our next issue. :-)

    Thank you very much,

    David
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    17 Aug 2009
    Link to this post
    Hello David,

    Always glad to help. You can mark the answers so that this will help to the other community members if they got stuck. We look forward to hearing from you.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    17 Aug 2009
    Link to this post
    Hello,

    For anyone facing a similar problem, here is basically the code I'm using.

    C#
    //Instantiate UserMangare and ProfileBase 
    UserManager um = new UserManager(); 
    ProfileBase pb = new ProfileBase(); 
     
    //Get an array of strings of the usernames in the role we choose 
    string[] users = um.GetUsersInRole("rolename"); 
     
    //Cycle through all of the users 
    foreach(string user in users) 
     { 
     //Initialize the ProfileBase for the given user 
        pb.Initialize(user, true); 
        
        //Put any display logic you want to use 
        // Here are some possible values to return 
          // First name: pb.GetPropertyValue("FirstName") 
          // Last name: pb.GetPropertyValue("LastName") 
          // Photo: pb.GetPropertyValue("Photo")  -- Returns the source 
        // For complex groups, it will look similar to this structure: 
         // Phone Number: pb.GetProfileGroup("Phone").GetPropertyValue("Value") 
         // Instant Messenger: pb.GetProfileGroup("InstantMessenger").GetPropertyValue("IM_Type") -- Returns the type of instant messenger (AIM, ICQ, MSN, etc.) 
        // IM Name: pb.GetProfileGroup("InstantMessenger").GetPropertyValue("IM_Value") -- Returns your IM name 
      } 

    To implement this for all users, not just in a specific role, it wouldn't be too much different. Instead of using um.GetUsersInRole to return an array of strings, you can use um.GetAllUsers to return a MembershipUserCollection instead.

    I hope it is helpful!

    David
Register for webinar
8 posts, 1 answered