C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

Last post 09-17-2009 3:44 PM by Cadeey. 8 replies.

Sort Posts:

  • C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    07-27-2006, 9:38 AM
    • Member
      30 point Member
    • Tomek Prentki
    • Member since 06-22-2006, 1:46 PM
    • Rotterdam, The Netherlands
    • Posts 6

     

    The example below show how easy can get different roles groups and users by using SPWeb object. This example is split in thee levels.

     

    First Level:

    Get all users, groups, roles from SharePoint.

     

    Second Level:

    When the users exist in you selected SPWeb object, all specific information should me inserted into generic class UserListCollection.

     

    Third Level:

    Example class below shows the algorithms, how you get the user information from ArrayList object and cast the object into UserListCollection.

     

     

    using System;

    using System.Collections;

     

    using Microsoft.SharePoint;

    using Microsoft.SharePoint.Administration;

     

    namespace Framework.Business.Components

    { 

                /// <summary>

                /// UsersCollection is object to get all users by selected web object.

                /// </summary> 

                public class UsersCollection

                {

                            private ArrayList    _usersListCollection;   //All incomming users.

                            private SPWeb       _selectedWeb;           //Web position.

                            private string         _groupName;            //Group name.

      

                            private const string _allGroups  = "ALL_GROUPS";

     

                            /// <summary>

                            /// SPWebLocation property.

                            /// </summary>

                            public SPWeb SPWebLocation

                            {

                                       get{return _selectedWeb;}

                                       set{_selectedWeb = value;}

                            }

     

                            /// <summary>

                            /// SPGroupName property.

                            /// </summary>

                            public string SPGroupName

                            {

                                       get{return _groupName;}

                                       set{_groupName = value;}

                            }

     

                            /// <summary>

                            /// To run the example you need to initialize the UsersCollection object with two instance website and GroupName objects.

                            /// </summary>

                            /// <param name="webSite">SPWeb object</param>

                            /// <param name="groupName">string group name</param>

                            public UsersCollection(SPWeb webSite ,string groupName)

                            {    

                                       //Check if the SPWeb object is not null.

                                       if(webSite != null)

                                       {

                                                   if(groupName == null || groupName.Length == 0)

                                                   {     

                                                               this.SPGroupName = _allGroups;

                                                   }

                                                   _usersListCollection = new ArrayList();

     

                                       }

                                       else

                                       {

                                                   Console.WriteLine("ERROR WEB LOCATION: Your web location have a null value.");

                                       }

                            }

     

                            /// <summary>

                            /// This public method get all users from SharePoint web location and get user related information.

                            /// </summary>

                            public ArrayList GetUsersByGroup()

                            {

                                       if(this.SPGroupName == _allGroups)

                                       {

                                                   foreach(SPGroup singleGroup in this.SPWebLocation.Groups)

                                                   {

                                                               UsersList(singleGroup);

                                                   }

                                       }

                                       else

                                       {

                                                   SPGroup singleGroup = this.SearchGroup(this._groupName);

                                                   if(singleGroup != null)

                                                   {

                                                               this.UsersList(singleGroup);

                                                   }

                                                   else

                                                   {

                                                               Console.WriteLine("ERROR WEB GROUP: " +  this._groupName + " GROUP NOT EXIST.");

                                                   }

                                       }

                                       return _usersListCollection;

                            }

     

                            /// <summary>

                            /// This private method get users by selected SPGroup object.

                            /// </summary>

                            /// <param name="group">SPGroup object</param>

                            private void UsersList(SPGroup group)

                            {

                                       foreach(SPUser singleUser in group.Users)

                                       {

                                                   foreach(SPRole singleRole in singleUser.Roles)

                                                   {

                                                               _usersListCollection.Add(new UserListCollection(

                                                                   singleUser.LoginName,singleRole.Name,group.ParentWeb.Title));

                                                   }

                                       }

                            }

     

                            /// <summary>

                            /// This method search for SPGroup name.

                            /// </summary>

                            /// <param name="group">string</param>

                            /// <returns>SPGroup object</returns>

                            private SPGroup SearchGroup(string group)

                            {

                                       SPGroup groupObject = null;

                                       foreach(SPGroup singleGroup in this.SPWebLocation.Groups)

                                       {

                                                   if(group == singleGroup.Name)

                                                   {

                                                               groupObject = singleGroup;

                                                   }

                                       }

                                       return groupObject;

                            }

                }

       

                /// <summary>

                /// Generic class UserListCollection

                /// </summary> 

                public class UserListCollection

                {

                            private string _userName = "";

                            private string _userRole = "";

                            private string _userWeb = "";

     

                            /// <summary>

                            /// Generic username.

                            /// </summary>

                            public string UserName

                            {

                                       get { return _userName; }

                                       set { _userName = value; }

                            }

     

     

                            /// <summary>

                            /// Generic user role.

                            /// </summary>

                            public string UserRole

                            {

                                       get { return _userRole; }

                                       set { _userRole = value; }

                            }

     

                            /// <summary>

                            /// Generic SharePoint web name.

                            /// </summary>

                            public string WebName

                            {

                                       get { return _userWeb; }

                                       set { _userWeb = value; }

                            }

     

                            /// <summary>

                            /// This constructor insert three values.

                            /// </summary>

                            /// <param name="userName">Name of NT user account.</param>

                            /// <param name="userGroup">SharePoint web group name.</param>

                            /// <param name="userWeb">SPSite web name.</param>

                            public UserListCollection(string userName, string userGroup, string userWeb)

                            {

                                       this.UserName   = userName;

                                       this.UserRole     = userGroup;

                                       this.WebName   = userWeb;

                            }

                }

     

                /// <summary>

                /// Example to run UsersCollection class.

                /// </summary>

                class Example

                {

                            /// <summary>

                            /// Example to get all users info with generic UsersCollection class.

                            /// </summary>

                            public static void GetUsers()

                            {

                                       //Url location object.

                                       Uri url = new Uri("http://localserver/");

     

                                       SPGlobalAdmin globalAdmin = new SPGlobalAdmin();

                                       SPVirtualServer  virtualServer = globalAdmin.OpenVirtualServer(url);

     

                                       //Get all users by site position and group.

                                       UsersCollection user = new UsersCollection(

                                                   virtualServer.Sites[0].AllWebs[0],"Reader");

     

                                       ArrayList usersArray = user.GetUsersByGroup();

                                       for(int i = 0;i < usersArray.Count;i++)

                                       {

                                                   //Generic class

                                                   UserListCollection userExample = (UserListCollection)usersArray[i];

                                                   Console.WriteLine("UserName: " + userExample.UserName);

                                                   Console.WriteLine("Role: " + userExample.UserRole);

                                                   Console.WriteLine("Web: " + userExample.WebName);

                                       }

                            }

                }

    }

     

     

     

    Tomek Prentki
    E-mail:tomasz_prentki@hotmail.com
  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    01-14-2008, 12:18 AM
    • Member
      7 point Member
    • obajaber
    • Member since 12-04-2005, 9:43 PM
    • Posts 2
    Thanks a lot! This guided me when I was doing something slightly different but basically using the same approach.
  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    04-07-2009, 4:53 AM
    • Member
      2 point Member
    • npcs
    • Member since 04-07-2009, 8:52 AM
    • Posts 1

     

    It all looks good, but how do I call it? Can you give an example of the correct parameter names?

    Thanks :)

  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    04-24-2009, 9:49 AM
    • Member
      16 point Member
    • Userr_
    • Member since 08-04-2008, 9:08 PM
    • Posts 73

    Hello there,

     

    Thanks alot for sharing your solutoin.

     

    I'm trying to show list of WSS3.0 users on a web part. the users from the WSS3.0 groups and I find that what you have done is very close to what I need.

    I have created a web Part template in VS2008, the Web Part is build and deployed to WSS3.0 site and it works fine.

    I placed your code into my .cs file and added the required references, this time when I deploy the web part notthing happens, I was expecting to see the users in my web part!!

     Am I missing something or What? Can you please help me?

     

    Thank alot

    Filed under: , ,
  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    05-13-2009, 12:12 AM
    • Member
      2 point Member
    • serenade
    • Member since 05-13-2009, 4:08 AM
    • Posts 1
    Hi there, Im doing something similar to that too. Also using WSS 3.0 and Visual Studio 2008. Have you achieve what you want already? Can you please give some guides/tips or so? Thanks alot
  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    05-16-2009, 7:04 AM
    • Member
      4 point Member
    • AjayKN
    • Member since 05-16-2009, 11:00 AM
    • Posts 2

    Hi Tomek Prentki,

    Please let me know what to do for FBA share point site, where do I need to pass user credentials?

    Thanking you

    Ajay K.N
    Filed under: ,
  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    07-06-2009, 8:35 PM

     Thanks so much for giving me something to work with. I'm developing in InfoPath and working feverishly to get back user and group information.

    These things should be a piece of cake for InfoPath--but just look at posts on usergroup.asmx.

    You've given me something work with to develope my web service.

     

    thank-you,

     

    Stephan Onisick

    stephan.onisick@att.net

    Filed under:
  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    09-15-2009, 10:32 AM

     Hi Tomek Prentki ,

    I used this code for getting the usergroup from ours sharepoint site . But it is failing we are getting usergroup null  when running this code.  Please do needful.

    Thanks,

    Govind

  • Re: C# Example: How to get all groups, users and roles from SharePoint using SPGroup, SPUser, SPRole objects.

    09-17-2009, 3:44 PM
    • Member
      98 point Member
    • Cadeey
    • Member since 01-30-2008, 3:18 PM
    • Posts 53

    What I am trying to do is: user logins in then in the main page a calendar shows up with user's projects listed in the calendar.  Projects to display in calendar depends on login user.  Any idea on how to implement that???  I'm using oracle database to authenticate users as they login.

Page 1 of 1 (9 items)