serving the solutions day and night

Pages

Tuesday, December 28, 2010

Generate SharePoint People(Users),Groups & Permissions List

C# code will generate users, groups and permissions list from site collection web sites. It means the code will get all people, groups and their role information from People and Group pages for each web site in a single report instead of browsing each pages.
  1. Create siterole.aspx, copy the following code in it.
  2. Paste the siterole.aspx file in the c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS directory.
  3. Browse the page http://{your URL NAME}/{site NAME}/_layouts/siterole.aspx and view the report.

<%@ 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">

<script runat="server">
protected string CheckWebIsRoot(SPWeb aWeb)
{
if (aWeb.IsRootWeb)
{
return " (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)
{
Response.Write("
<u>Reports on web site " + aWeb.Title + "" + CheckWebIsRoot(aWeb) + "");
Response.Write("
<br>");

foreach (SPRoleAssignment aRole in aWeb.RoleAssignments)
{
isAgroup = true;
try
{
aGroup = aWeb.Groups.GetByID(aRole.Member.ID);
}
catch
{
isAgroup = false;
}

if (isAgroup)
{
Response.Write("
Group ID -> <u>" + aRole.Member.Name +"");
Response.Write("
");

Response.Write("Number of users:" + aWeb.Groups.GetByID(aRole.Member.ID).Users.Count);
Response.Write("
<br>");

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

if (aWeb.Groups.GetByID(aRole.Member.ID).Users.Count>0)
{
Response.Write("List of " + aGroup.Name + " users");
Response.Write("
");

foreach (SPUser aUser in aGroup.Users)
{
Response.Write(" - " + aUser.Name);
Response.Write("
");
}

Response.Write("
");

}
}
else
{
if (aRole.RoleDefinitionBindings.Count>0)
{
Response.Write("User ID -> " + aRole.Member.Name + " ( ");
for (int i = 0; i < aRole.RoleDefinitionBindings.Count; i++)
{
Response.Write(aRole.RoleDefinitionBindings[i].BasePermissions.ToString());
}
Response.Write(" )<br>");
}
}
}
Response.Write("
");
}
}

</script>

</asp:Content >












1 comment:

Anonymous said...

How would this work for SP 2010 or foundation product?