I'm writing an InfoPath form that upon loading will Query Active Directory (based on the currently logged in user) and auto-populate the fields in the form.
I've been able to successfully query AD for the information, the problem I'm running into is when there is a field in Active Directory that does not contain data. I found if i comment out the lines that are pointing towards info that isn't in AD everything runs fine.
I tried to alter the code to add in an IF-ELSE statement, hoping that it would check to see if the field was null and if so leave it blank, if data does happen to exist then display it.
The field i'm having problems with right now is the "Title" field, if i can figure my way around that one, i'm sure i can apply it to the rest of the fields.
So if anyone out there could help me manipulate this code so that it either displays the information from the AD fields or if it's blank to just display nothing and not error out.
I am not a programmer of any sort, i just copied this sample code from the MSDN site. I've taken a few programming classes a while back, but it is definitely not my forte. So if you could explain it as simply and throughly as possible, i would greatly appreciate it.
CODE SAMPLE:
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.DirectoryServices;
using System.Windows.Forms;
namespace AD_Connection
{
public partial class FormCode
{
// Member variables are not supported in browser-enabled forms.
// Instead, write and read these values from the FormState
// dictionary using code such as the following:
//
// private object _memberVariable
// {
// get
// {
// return FormState["_memberVariable"];
// }
// set
// {
// FormState["_memberVariable"] = value;
// }
// }
// NOTE: The following procedure is required by Microsoft Office InfoPath.
// It can be modified using Microsoft Office InfoPath.
public void InternalStartup()
{
EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
}
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
// Get the user name of the current user.
string strUserName = 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=" + strUserName + ")");
// Search for the specified user.
SearchResult result = searcher.FindOne();
// Make sure the user was found.
if (result == null)
{
MessageBox.Show("Error finding user: " + strUserName);
}
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 strFirstName = employee.Properties["givenName"].Value.ToString();
string strLastName = employee.Properties["sn"].Value.ToString();
string strMail = employee.Properties["mail"].Value.ToString();
string strLocation = employee.Properties["physicalDeliveryOfficeName"].Value.ToString();
string strTitle = string.Empty;
string strPhone = employee.Properties["telephoneNumber"].Value.ToString();
string strDepartment = 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:FirstName", ns)
.SetValue(strFirstName);
xnMyForm.SelectSingleNode("/my:myFields/my:LastName", ns)
.SetValue(strLastName);
xnMyForm.SelectSingleNode("/my:myFields/my:Alias", ns)
.SetValue(strUserName);
xnMyForm.SelectSingleNode("/my:myFields/my:Email", ns)
.SetValue(strMail);
xnMyForm.SelectSingleNode("/my:myFields/my:Location", ns)
.SetValue(strLocation);
if (strTitle != null)
{
strTitle = employee.Properties["title"].Value.ToString();
xnMyForm.SelectSingleNode("/my:myFields/my:Title", ns)
.SetValue(strTitle);
}
else
{
strTitle = string.Empty;
}
xnMyForm.SelectSingleNode(
"/my:myFields/my:TelephoneNumber", ns)
.SetValue(strPhone);
xnMyForm.SelectSingleNode("/my:myFields/my:Department", ns)
.SetValue(strDepartment);
// Clean up.
xnMyForm = null;
searcher.Dispose();
result = null;
employee.Close();
}
}
}
}