Saturday, June 13, 2009

How to populate a custom Contact List from AD

In a recent project at work, I was required to create a custom Contact List. The only problem was that I didn't want my SharePoint admins to have to go to the list, then click New -> Item and have to sit there and enter in the person's name, email address, title, etc, when all this information is already sitting over in AD (Active Directory) to begin with.

So, I wanted to provide my SharePoint site admins with the ability to search AD, select a person (or people), then with the click of a button, add them into my Contact List, which is a custom list library. My solution ended up being quite straightforward - create a web part with two controls, 1. a PeopleEditor control (the standard SharePoint control for selecting people from AD; it has those two little buttons in the lower right for checking the name you enter and a browse button to let you do searches) and 2. a Button control which will take whatever people have been entered into the PeopleEditor and save them into the Contact List.

First off, this assumes you have created a custom list called "Contact List" on your SharePoint site. The fields are all text fields, called "Name", "Job Title", and "Email Address".

1. Load Visual Studio 2008
2. Click File -> New -> Project... -> Visual Basic -> SharePoint -> Web Part
3. For the name, I called mine AddContactFromAD
4. Notice the web part was named WebPart1. Do a search and replace across the whole project and change WebPart1 to AddContactFromAD wherever it is found. While that just changed the contents of the files in the project, you will notice there is still a folder called WebPart1 with files inside all named WebPart1 with different file extensions. Manually rename the folder and the base names of those files (leaving the extensions alone for each).
5. Copy and paste the code below.
6. In the Solution Explorer, right click the project and click Properties, click the Debug tab on the left, then in "Start browser with URL:" type in the name of the SharePoint site you want to deploy the web part to.
7. Hit F5 to build and deploy the web part.

Code for AddContactFromAD.vb:

1. under your inherits statement and above your constructor, copy and paste:

WithEvents btnAddUsers As New Button
Dim peADLookUp As New PeopleEditor

2. in CreateChildControls(), copy and paste:

'create and add message to web part
Dim lblMessage As New Label
lblMessage.Text = "Add contact(s) from Active Directory into Contact List"
Controls.Add(lblMessage)

'add PeopleEditor control to web part (it was created above)
peADLookUp.Rows = 1
Controls.Add(peADLookUp)

'add button to web part (it was created above)
btnAddUsers.Text = "Add user(s) to Contact List"
Controls.Add(btnAddUsers)

3. below CreateChildControls(), copy and paste the following subroutine.

Sub btnAddUsers_Click() Handles btnAddUsers.Click
'PURPOSE: add selected users to Contact List

'get the site the web part is running on
Dim oWeb As SPWeb
oWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context)

'get the list where we want to save the person to
Dim oList As SPList
oList = oWeb.Lists("Contact List")


Dim strDisplayName As String = ""
Dim strEmail As String = ""
Dim strTitle As String = ""

'loop through each entity in peADLookUp (note DisplayName,
' Email, and Title are all properties in AD)
For Each account As PickerEntity In peADLookUp.ResolvedEntities
'get values of current entity
strDisplayName = account.EntityData("DisplayName").ToString()
strEmail = account.EntityData("Email").ToString()
strTitle = account.EntityData("Title").ToString()

'create a new list item in Contact List
Dim oNewItem As SPListItem
oNewItem = oList.Items.Add()

'populate the new list item with values from the form
oNewItem.Item("Name") = strDisplayName
oNewItem.Item("Job Title") = strTitle
oNewItem.Item("Email Address") = strEmail

'update the list with the new item
oNewItem.Update()
Next

End Sub

14 comments:

  1. You make it look easy! Thanks!

    ReplyDelete
  2. Great and helpful realy! thanks !

    ReplyDelete
  3. D-J,

    Thanks for this post. It indeed is very helpful.

    However, I am unable to load the contact information into the "Contact List" I have created. The "AddContactFromAD" webpart is able to search AD and retrieve the user information, but when onclick function triggers, I get an error:

    "An unexpected error has occurred.
    Web Part Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator."

    I wonder where am I going wrong. Will appreciate your help.

    Thanks much.

    ReplyDelete
  4. Just curious if there was a solution to Khaja's problem. I just tried this and ran into the same issue.

    Thanks

    ReplyDelete
  5. The first thing to do is figure out which line is causing the problem. Can you debug this web part and set a breakpoint starting at the point we are setting oWeb? OR, just comment out all the code below where we are setting oWeb, run it, see if it bombs out, then if it doesn't, slowly add code back in and keep trying it until you get the code to bomb out. It must be something specific to your set up (and Khaja's). For that matter, this could easily be happening to others, so I would definitely like to figure this out so I can amend this article for others. I should have tackled this when Khaja asked months ago, but I have been pretty busy. Try that and tell me which line is causing us problems. Thanks Steve.

    ReplyDelete
  6. Hi,

    I am facing one problem...

    I want to add the columns that are shown in the dialog of the People Picker. How can i control that. When i do PeoplePicker.ResolvedEntities[0].EntityData...where is this data coming from...?

    Is it coming from Active Directory...?

    If Yes, then how can i make sure that "Email" will always be the part of the EntityData collection...

    thanks,
    Email - shakti.ietk.85@gmail.com

    ReplyDelete
  7. Shakti,
    As far as I know, Email should be there. Are you saying its not for you? If so, maybe there is a setting in AD that needs to be changed. In my code above, Email is retrieved from AD automatically in the PeopleEditor's ResolvedEntities collection. Then it gets put in the EntityData collection of PickerEntity object. I don't recall needing to do anything special to get Email to show up. It was available automatically.

    ReplyDelete
  8. Does this automatically sync modified AD data to a contact already in Sharepoint?

    ReplyDelete
  9. No, the list item will only contain the contacts' values from when they were first selected from AD. However, one way to implement the functionality you want is to create a timer job that loops through the contacts you've added to the list and checks AD for updates. I wouldn't be surprised if there is a much better way of doing it now, especially with SharePoint 2010. If someone knows a better way, let us know!

    ReplyDelete
  10. Can I populate this information using SharePoint designer?

    ReplyDelete
    Replies
    1. I have managed to get a solutions around this, thanks.

      Delete
    2. HI Mthuthuzeli What as the solution, i am trying to also populate a SharePoint field with the users name from active directory using SharePoint designer, how is it done?

      Regards

      Delete
  11. Remarkable! Its in fact awesome article, I have got much clear idea regarding from this piece of writing.

    ReplyDelete
  12. I have been browsing on-line more than three hours these days, yet I by no means found any interesting article like yours. It is beautiful value enough for me. Personally, if all web owners and bloggers made just right content material as you did, the net will be much more useful than ever before.

    ReplyDelete