Software Architecture – SharePoint 2010 – To list the Active Directory groups which are used for assignment of permission

Objective: As in previous blog entry here , we have created a new menu item in the permission tab of ribbon control, now on click of that menu we want to open a custom page residing in the layout folder which can list AD groups which are used to assign rights in this web site of SharePoint. On clicking on any AD group name a third party group management solution will be opened in new window.
Create a new aspx page with following code in layout folder of sharepoint.

<%@ Assembly Name="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Assembly Name="System.DirectoryServices, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>
<%@ Assembly Name="System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"%>

<%@ Page Language="C#"          Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase"  %>

<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace="System.Data" %>

<script runat="server">
    public enum objectClass
        user, group, computer
    public enum returnType
        distinguishedName, ObjectGUID
protected override void OnLoad(EventArgs e) {
SPWeb site = this.Web;
lblSiteTitle.Text = site.Title;
lblSiteID.Text = site.ID.ToString();
System.Collections.Generic.List<SPUser> lstADGroups = new System.Collections.Generic.List<SPUser>();
foreach (SPUser item in site.Users)
    if (item.IsDomainGroup)
grdADGroups.DataSource = lstADGroups;

    public string GetObjectDistinguishedName(objectClass objectCls,
    returnType returnValue, string objectName, string LdapDomain)
    string distinguishedName = string.Empty;
    string connectionPrefix = "LDAP://" + LdapDomain;
    DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
    DirectorySearcher mySearcher = new DirectorySearcher(entry);

    switch (objectCls)
        case objectClass.user:
            mySearcher.Filter = "(&(objectClass=user) (|(cn=" + objectName + ")(sAMAccountName=" + objectName + ")))";
            mySearcher.Filter = "(&(objectClass=group)  (|(cn=" + objectName + ")(dn=" + objectName + ")))";
            mySearcher.Filter = "(&(objectClass=computer)  (|(cn=" + objectName + ")(dn=" + objectName + ")))";
    SearchResult result = mySearcher.FindOne();

    if (result == null)
        throw new NullReferenceException
        ("unable to locate the distinguishedName for the object " +
        objectName + " in the " + LdapDomain + " domain");
    DirectoryEntry directoryObject = result.GetDirectoryEntry();
    if (returnValue.Equals(returnType.distinguishedName))
        distinguishedName = "LDAP://" + directoryObject.Properties
    if (returnValue.Equals(returnType.ObjectGUID))
        distinguishedName = directoryObject.Guid.ToString();
    return distinguishedName;
protected void grdADGroups_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
        SPUser rowView = (SPUser)e.Row.DataItem;
        HyperLink hLink = (HyperLink)e.Row.FindControl("Edit");
        hLink.Text = "Edit Membership";
        DirectoryEntry ent = new DirectoryEntry();
        char[] key = new char[1];
        key[0] = '\\';
        String GroupName = rowView.LoginName.Split(key)[1].ToString();
       string dname= GetObjectDistinguishedName(,returnType.distinguishedName, GroupName, rowView.LoginName.Split(key)[0].ToString());
        dname= dname.Substring(7,dname.Length-7);
        hLink.NavigateUrl = "http://win-2k8-r2-64/WebShah/properties.aspx?dn=" + dname   ;


<form runat="server">
<table border="1" cellpadding="4" cellspacing="0" style="font-size:12">
<tr><td colspan="2"><b>Custom GroupManagement solution </b> - Custom GroupManagement solution,Custom GroupManagement solution,Custom GroupManagement solution,Custom GroupManagement solution.</td></tr>
      <td>Site Title:</td>
      <td><asp:Label ID="lblSiteTitle" runat="server" /></td>
      <td>Site ID:</td>
      <td><asp:Label ID="lblSiteID" runat="server" /></td>
  <br />
  <asp:GridView ID="grdADGroups" runat="server" CellPadding="3" 
            EnableModelValidation="True" GridLines="Vertical" 
            AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" 
            BorderStyle="None" BorderWidth="1px" 
            onrowdatabound="grdADGroups_RowDataBound" >
            <AlternatingRowStyle BackColor="#DCDCDC" />
                <asp:BoundField AccessibleHeaderText="Group Name" DataField="LoginName" 
                    HeaderText="Group Name" />
                <asp:TemplateField HeaderText="Modify membership">     
                 <asp:HyperLink ID="Edit" runat="server" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />

The above code is self-explanatory.
Happy coding SharePiont.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: