Showing posts with label Get all groups. Show all posts
Showing posts with label Get all groups. Show all posts

Sunday, December 2, 2012

Get all groups,roles and users details in SharePoint

Get All Groups, Users and Roles in SharePoint:

In this example i wanna show how to get all groups , users and roles in SharePoint site.

            StringBuilder sb = new StringBuilder();
            using (SPSite site = new SPSite("http://ukreddy:6969/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPGroupCollection groupCollection = web.SiteGroups;
                    foreach (SPGroup group in groupCollection)
                    {
                        SPUserCollection userCollection = group.Users;
                        sb.Append("Group Name :" + group.Name + "\n");
                        foreach (SPUser user in userCollection)
                        {
                            sb.Append("User Name: " + user.Name + " ; Email: " + user.Email + " ; Login: " + user.LoginName);
                            sb.Append("\n");
                        }
                        SPRoleCollection sprolecol = group.Roles;
                        foreach (SPRole role in sprolecol)
                        {
                            sb.Append("Role Name: " + role.Name + " ; Role Desc: " + role.Description + "\n");
                        }
                        sb.Append("\n");
                    }
                   MessageBox.Show(sb.ToString());
                }
            }