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.
Implementation:
Create a new aspx page with following code in layout folder of sharepoint.

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>
<%@ Assembly Name="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>
<%@ Assembly Name="System.Data, Version=2.0.0.0, 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)
        lstADGroups.Add(item);
}
grdADGroups.DataSource = lstADGroups;
grdADGroups.DataBind();

  }
    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 + ")))";
            break;
        case objectClass.group:
            mySearcher.Filter = "(&(objectClass=group)  (|(cn=" + objectName + ")(dn=" + objectName + ")))";
            break;
        case objectClass.computer:
            mySearcher.Filter = "(&(objectClass=computer)  (|(cn=" + objectName + ")(dn=" + objectName + ")))";
            break;
    }
    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
            ["distinguishedName"].Value;
    }
    if (returnValue.Equals(returnType.ObjectGUID))
    {
        distinguishedName = directoryObject.Guid.ToString();
    }
    entry.Close();
    entry.Dispose();
    mySearcher.Dispose();
    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(objectClass.group,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   ;
    }
}

</script>

<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>
    <tr>
      <td>Site Title:</td>
      <td><asp:Label ID="lblSiteTitle" runat="server" /></td>
    </tr>
    <tr>
      <td>Site ID:</td>
      <td><asp:Label ID="lblSiteID" runat="server" /></td>
    </tr>
  </table>
  <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" />
            <Columns>
                <asp:BoundField AccessibleHeaderText="Group Name" DataField="LoginName" 
                    HeaderText="Group Name" />
                
                <asp:TemplateField HeaderText="Modify membership">     
                <ItemTemplate>
                 <asp:HyperLink ID="Edit" runat="server" />
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <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" />
        </asp:GridView>
        </form>

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: