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

 

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

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

{

///

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

///

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”;

///

/// SPWebLocation property.

///

public SPWeb SPWebLocation

{

get{return _selectedWeb;}

set{_selectedWeb = value;}

}

///

/// SPGroupName property.

///

public string SPGroupName

{

get{return _groupName;}

set{_groupName = value;}

}

///

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

///

/// SPWeb object

/// string group name

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.”);

}

}

///

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

///

public ArrayList GetUsersByGroup()

{

if(this.SPGroupName == _allGroups)

{

foreach(SPGroup singleGroup inthis.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;

}

///

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

///

/// SPGroup object

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));

}

}

}

///

/// This method search for SPGroup name.

///

/// string

/// SPGroup object

private SPGroup SearchGroup(string group)

{

SPGroup groupObject = null;

foreach(SPGroup singleGroup inthis.SPWebLocation.Groups)

{

if(group == singleGroup.Name)

{

groupObject = singleGroup;

}

}

return groupObject;

}

}

///

/// Generic class UserListCollection

///

public class UserListCollection

{

private string _userName = “”;

private string _userRole = “”;

private string _userWeb = “”;

///

/// Generic username.

///

public string UserName

{

get { return _userName; }

set { _userName = value; }

}

///

/// Generic user role.

///

public string UserRole

{

get { return _userRole; }

set { _userRole = value; }

}

///

/// Generic SharePoint web name.

///

public string WebName

{

get { return _userWeb; }

set { _userWeb = value; }

}

///

/// This constructor insert three values.

///

/// Name of NT user account.

/// SharePoint web group name.

/// SPSite web name.

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

{

this.UserName = userName;

this.UserRole = userGroup;

this.WebName = userWeb;

}

}

///

/// Example to run UsersCollection class.

///

class Example

{

///

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

///

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);

}

}

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *