Tyler Adams

Tyler Adams

  • NA
  • 10
  • 0

Retrieving info from Active Directory (null fields)

Feb 4 2009 5:27 PM

I have some code in an InfoPath form that retrieves user information from Active Directory.  My problem is that when it tries to retrieve the information and one of the fields in the AD users properties is blank the form displays an error and when it opens it doesn't autopopulate any of the fields.

How can i get the code to simply display a blank field and open the form without errors and populate all the fields that have information and the ones that don't have information, just leave blank so the user can fill them in?

 

Here is my code snippet (It loads fine for me, but if i go and take out the information in the Address field and reopen the form i get the error.)

public void InternalStartup()

{

((ButtonEvent)EventManager.ControlEvents["SubmitButton"]).Clicked += new ClickedEventHandler(SubmitButton_Clicked);

EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);

}

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

try

{

// Get the user name of the current user.

string userName = this.Application.User.UserName;

// Create a DirectorySearcher object using the user name

// as the LDAP search filter. If using a directory other

// than Exchange, use sAMAccountName instead of mailNickname.

DirectorySearcher searcher = new DirectorySearcher(

"(mailNickname=" + userName + ")");

// Search for the specified user.

SearchResult result = searcher.FindOne();

// Make sure the user was found.

if (result == null)

{

MessageBox.Show("Error finding user: " + userName);

}

else

{

// Create a DirectoryEntry object to retrieve the collection

// of attributes (properties) for the user.

DirectoryEntry employee = result.GetDirectoryEntry();

// Assign the specified properties to string variables.

string FullName = employee.Properties[

"displayName"].Value.ToString();

string Mail = employee.Properties["mail"].Value.ToString();

string Location = employee.Properties[

"physicalDeliveryOfficeName"].Value.ToString();

string Address = employee.Properties[

"streetAddress"].Value.ToString();

string MailStop = Address.Substring(22);

string Phone = employee.Properties[

"telephoneNumber"].Value.ToString();

string Department = employee.Properties[

"department"].Value.ToString();

// Create an XPathNavigator to walk the main data source

// of the form.

XPathNavigator xnMyForm = this.CreateNavigator();

XmlNamespaceManager ns = this.NamespaceManager;

// Set the fields in the form.

xnMyForm.SelectSingleNode("/my:myFields/my:RequestorFullName", ns)

.SetValue(FullName);

xnMyForm.SelectSingleNode("/my:myFields/my:RequestorEmail", ns)

.SetValue(Mail);

xnMyForm.SelectSingleNode("/my:myFields/my:RequestorOffice", ns)

.SetValue(Location);

xnMyForm.SelectSingleNode("/my:myFields/my:RequestorMailstop", ns)

.SetValue(MailStop);

xnMyForm.SelectSingleNode("my:myFields/my:RequestorExtension", ns)

.SetValue(Phone);

xnMyForm.SelectSingleNode("/my:myFields/my:RequestorDepartment", ns)

.SetValue(Department);

// Clean up.

xnMyForm = null;

searcher.Dispose();

result = null;

employee.Close();

}

}

catch (Exception ex)

{

MessageBox.Show("The following error occurred: " +

ex.Message.ToString());

throw;

}

}


Answers (1)