Sunteți pe pagina 1din 13

How to Access to AD

using .Net from


SharePoint 2013
1 Introduction
This asset has as objective to show how we have to do to access to Active
Directory (in advance AD) using .Net from a SharePoint Server 2013.
We are going to create a WebPart where we are going to write code in c# in
order to show groups and users located in AD (Org Chart). For be able to realize this
work we need a virtual machine with SharePoint Server 2013 installed.

2 Configuring AD
At first sight this is how our AD looks like:

Ilustracin 1 Active Directory

Now we are going to create an Organization Unit structure with groups and
users. For example I am going to user this structure:

We have an Organization Unit named Avanade.


Inside Avanade we have another Organization Unit named Malaga.
Inside Malaga we have two Organization Units named Groups and
Users.
Inside Users we have six users:
o joaquin-matias,
o javi-herrador,
o irene-duarte,
o greg-mao,
o eduardo-desanjose,
o alfonso-torres
Inside Groups we have three groups: COE, COE-MEM and HR
COE members COE-MEM, javi-herrador and joaquinmatias.
o COE-MEM members joaquin-matias, javi-herrador, greg-mao,
eduardo-desanjose, alfonso-torres.
HR members irene-duarte

The AD structure is shown in the images below. For each user we have to
insert some configuration for the description, telephone, email, etc. In this case we
are going to load information only for these fields.

Ilustracin 2 AD Groups

Ilustracin 3 AD Users

For creating an Organization Unit we have to click with right bottom and in
the new section click on Organization Unit. A new window is opened and there we
have to write a name for the Organization Unit.
For creating groups we have to click with right bottom and in the new section
click on Group. A new window is opened and there we have to insert the name of
the group. In addition in the Group scope section we have to select Universal.
For creating users we have to do the same but we have to choose User
instead of Group.

3 AD Considerations
In order to connect to AD and get groups and users we have to create a
connection from .Net. This connetion has the follow LDAP structure:
LDAP://ObjectNaming=value,ObjectNaming=value2,,ObjectNaming=valuen
Where ObjectNaming has the follow range of values:
-

OU = Organization Unit.
CN = Common Name.
DC = Domain Component.

And the values are names for each component.

For example, for our system, if we want to search all the groups in the Org Chart
we have created the connection will be:
LDAP://OU=Groups,OU=Malaga,OU=Avanade,DC=coecollabmlg,DC=com

4 Configuring Environment
We have to create a site collection where we are going to locate the WebPart
that will show the Org Chat. For doing that go to Central Administration and under
Application Management click on Create site collections and create the site
collection as shown in the illustration below.

Ilustracin 4 Create Site Collection

5 Creating the WebPart


Now that we have configured the environment we are going to create the code
that is going to create the Org Chart acceding to AD.
1. Open Visual Studio.
2. Create a new Empty SharePoint project and name it OrgChart, for example.
3. Add a new element to the project. Add a new Visual WebPart and name it
OrgChartWP.
4. In the OrgChartWP.webpart file edit the title of the WebPart in order to show
Org Chart as name of the WebPart.
5. Add the System.DirectoryServices reference to the project

Ilustracin 5 Insert System.DirectoryServices reference

The code necessary for the .ascx file is shown in the illustration below:

5.1Coding the ASCX WebPart file

Ilustracin 6 acx code

As we can see in the illustration we have a literal control in order to dynamically


write properties for each user in tha org chart. In addition theres a treeview control
that has to implement two events;
-

OnTreeNodeExpanded: Used when user clicks a group node and it is


expanded.
OnSelectedNodeChanged: Used when user clicks an user in order to see
its properties.

5.2Coding the ASCX.cs WebPart file


-

Some properties

Ilustracin 7 Properties for the AD

We create two properties: the path to connect with AD and the groups that
are going to be created as headers. We have to mention that these properties can
be located wherever we want, for example as a WebPart configurable property.

Page_Load Event

Ilustracin 8 Page_Load event

As we can see the only thing we do here is to call the CreateHeaderNodes


method that is described above.

CreateHeaderNodes() method.

Ilustracin 9 CreateHeaderNodes() method

In this method we get groups and create the nodes for the treeview.
-

GetGroups() method.

Ilustracin 10 GetGroups() Part I

Here we create the connection with AD.

Ilustracin 11 GetGroups() Part II

In this part of the code we build the filter for bringing the groups that are
going to be the tree view headers.

Ilustracin 12 GetGroups() Part III

And in the last part of this method we get the groups and insert their names in a
list<string> object.

GetAllMembers(string group) method.

Ilustracin 13 GetAlMembers() method

This method is used to get all the members for a given group. It will be used
when user click on a group and expand it.

TreeNodeExpanded event

This event is trigged every time the user chick on a group and this group is
expanded. In this method we get the name of the group and get all members. For
each member a new node has to be created for the TreeView. That is what we do in
this method.
An important details is that if the node that is expanded has already child nodes (it
was populated time before) this event do nothing, only expand the node.

Ilustracin 14 TreeNodeExpand event

TreeNodeSelectedChange event

This event is trigged every time an user click on a user, that is the last element of
the tree. The code inside this event gets the name of the user and the path for
searching this user. It means that we can set a member of one of our groups an user
located in other groups in AD. It calculated the path dynamically where we have to
search the user. This event write the value of the properties for this user. In this
case we have used the description, the telephone, the email and the name of the
user.

Ilustracin 15 TreeNodeSelectedChange event

GetProperty() method.

This method is used as an auxiliary method in order to get the value of a property
inside of a SearchResult object.

Ilustracin 16 GetPRoperty() method

6 Testing the Solution


Deploy the solution with the WebPart we have created against the Site
Collection we have created above. Create a new Page and insert the WebPart.
Navigate through the nodes and select users Result must be similar to this:

Ilustracin 17 Org Chart view

S-ar putea să vă placă și