Wednesday, 5 September 2012

Get all Users, Groups and Roles from SharePoint

First :
Get all users, groups, roles from SharePoint.

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

Third :
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);
                                   }
                        }
            }
}

No comments:

Post a Comment