Sunday, May 25, 2008

Enumerate Role Assignments to retrieve Groups and Users Permissions in a Windows Sharepoint Services 3.0 or MOSS Site

Introduction:





The following .aspx page with C# in line code, enumerates all the roles assignments of a Windows SharePoint Services 3.0 or MOSS site collection and displays in DebugView Window for each web site:

  • The role member and reports if it is a Group or an User.
  • If it is a group, displays the number of users in the Group and the users list.
  • In any case, displays the Permissions list.


Why to use it?





When you want to check the users and the groups present in a Site Collection web sites and their role, it can take time doing it by browsing "People and Group" administration pages for each web site. It would be nice to display all the information in a single report. The following code sample will give you this kind of report, and it will be easier for you to reorder Users and Groups using it.



How to use it?





Copy the following code in an .aspx file.


Paste the file in the LAYOUTS diectory.


Start DebugView.


Browse the page with site administrator permissions from any site using the usual Application Page url (...myWebSite/_layouts/thisPage.aspx).


Check Report in DebugView window.



Code Sample:



<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" %>

 

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Import Namespace="System.Diagnostics" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="server"> 

<asp:Label ID="lblMessage" runat="server" />

    <script runat="server">       

        protected string WriteIsRootWeb(SPWeb aWeb){

            if (aWeb.IsRootWeb)

            {

                return " (This is the Site Collection Root Web)";

            }

            else

            {

                return "";

            }

        }

 

        public void Page_load(object sender, EventArgs e)

        {

            bool isAgroup = true;

            SPGroup aGroup=null;

 

            foreach (SPWeb aWeb in SPContext.Current.Site.AllWebs)

            {

 

                Debug.WriteLine("\n******************************************");

                Debug.WriteLine("Roles Assignments Report on web site " + aWeb.Title + WriteIsRootWeb(aWeb));

                Debug.WriteLine("******************************************\n");

 

 

                Debug.WriteLine("List of " + aWeb.Title + " Groups");

 

                foreach (SPGroup Group in aWeb.Groups)

                {

                    Debug.WriteLine(Group.Name + " ID: " + Group.ID);

                }

 

                Debug.WriteLine("");

 

                foreach (SPRoleAssignment aRole in aWeb.RoleAssignments)

                {

                    isAgroup = true;

                    Debug.WriteLine("*************\n");

                    try

                    {

                        aGroup = aWeb.Groups.GetByID(aRole.Member.ID);

                    }

                    catch

                    {

                        isAgroup = false;

                    }

 

                    if (isAgroup)

                    {

                        Debug.WriteLine("Group Id : " + aRole.Member.ID.ToString() + " | " + " Principal Name : " + aRole.Member.Name);

 

                        Debug.WriteLine("Number of users:" + aWeb.Groups.GetByID(aRole.Member.ID).Users.Count);

                        aGroup = aWeb.Groups.GetByID(aRole.Member.ID);

                        Debug.WriteLine("");

                        Debug.WriteLine("List of " + aGroup.Name + " users");

 

                        foreach (SPUser aUser in aGroup.Users)

                        {

                            Debug.WriteLine("\t - " + aUser.Name);

                        }

                        Debug.WriteLine("");

                    }

                    else

                    {

                        Debug.WriteLine("User Id : " + aRole.Member.ID.ToString() + " | " + " Principal Name : " + aRole.Member.Name);

                    }

 

                    Debug.WriteLine("\nList of permissions for " + aRole.Member.Name + ":");

 

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

                    {

                        Debug.WriteLine(aRole.RoleDefinitionBindings[i].BasePermissions.ToString());

                    }

 

                    Debug.WriteLine("");

                }

            }

            lblMessage.Text="Your report has been generated in DebugView";

        }

    </script>

</asp:Content >








if you want a much more complete report for SharePoint users and groups Permissions, Role Assignments within a SharePoint Site Collection, in HTML fomat generated inside an Application Page, see...




Enumerate Role Assignments to retrieve Groups and Users Permissions - Generating a complete report




if you want to know more about DebugView, see...




Use DebugView in Windows SharePoint Services 3.0 programming.






No comments:

Post a Comment