In this
article, we will come to know how to programmatically list all the users from
the SharePoint site.
Scenario:
We have to
retrieve all the Users from the
SharePoint site with the Group Name using object
model.
Solution:
I have created
a console application which will group the users with the Group Name and also
display the User details.
Code (.cs):
Code (.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace UserPermissions_Console
{
class Program
{
static void Main(string[] args)
{
try
{
GetUsersGroups();
}
catch (Exception ex)
{
Console.WriteLine("Error Occured: " + ex.Message);
}
}
private static void GetUserRoles()
{
using (SPSite site = new SPSite("http://sitename"))
{
SPWeb web = site.OpenWeb();
Console.WriteLine("\n\n Roles Assignments:");
foreach (SPRoleAssignment roleA in web.RoleAssignments)
{
Console.WriteLine("The following Role definition bindings exist for" + roleA.Member.Name);
foreach (SPRoleDefinition roledef in roleA.RoleDefinitionBindings)
{
Console.WriteLine(roledef.Name);
}
}
Console.ReadLine();
}
}
private static void GetUsersGroups()
{
using (SPSite site = new SPSite("http://sitename"))
{
SPWeb web = site.OpenWeb();
SPGroupCollection groupCollection = web.SiteGroups;
foreach (SPGroup group in groupCollection)
{
SPUserCollection userCollection = group.Users;
Console.WriteLine("Group Name :" + group.Name+"\n");
foreach (SPUser user in userCollection)
{
Console.WriteLine("User Name: " + user.Name + " Email: " + user.Email + " Login: " + user.LoginName);
}
}
//Iterate the owners group
SPGroup ownerGroup = web.AssociatedOwnerGroup;
foreach (SPUser ownerUser in ownerGroup.Users)
{
Console.WriteLine("User Name: " + ownerUser.Name + " Email: " + ownerUser.Email + " Login: " + ownerUser.LoginName);
}
}
Console.ReadLine();
}
}
}
You can download the full solution using this link.
Hope this helps you! Please free to comment and share this post.
No comments:
Post a Comment