Click to Rate and Give Feedback
MSDN
MSDN Library
Office Development
InfoPath 2007
Visual How Tos
 Adding User Information from Active...
Community Content
In this section
Statistics Annotations (10)
Adding User Information from Active Directory Domain Services to InfoPath 2007 Forms

Summary: Learn how to retrieve information about the current user of a Microsoft Office InfoPath 2007 form template from Active Directory Domain Services and add the information to the form.

Office Visual How To

Applies to: 2007 Microsoft Office System, Microsoft Office InfoPath 2007

Mark Roberts, Microsoft Corporation

November 2007

Overview

This Microsoft Office Visual How To describes how to get information about the current user of a Microsoft Office InfoPath 2007 form template from Active Directory Domain Services by using the DirectorySearcher class and DirectoryEntry class of the System.DirectoryServices namespace.

See It

Adding User Information to InfoPath 2007 Forms

Watch the Video

Length: 04:10 | Size: 3.65 MB | Type: WMV file

Code It | Read It | Explore It

Code It

The Microsoft.Office.InfoPath namespace provides the UserName property and the LoginName property of the User class. These properties let you retrieve the user name (and logon domain, if necessary) for a user from Active Directory Domain Services. You can also use the DirectorySearcher class and the DirectoryEntry class of the System.DirectoryServices namespace to obtain additional information about the user from Active Directory Domain Services.

To demonstrate this process, this section describes the following steps:

  • Creating an InfoPath form template that contains the fields in which to insert the data retrieved from Active Directory Domain Services.

  • Adding a Button control and Clicked event handler for testing the code that is used to get user information from Active Directory Domain Services and insert it into the fields on the form.

  • Adding a Loading event procedure that uses the same code to automatically fill in the fields when the form is opened.

  • Adding two fields and code to demonstrate how to enumerate all the attributes and values returned in the DirectoryEntry object for the current user.

Creating the Form

Before you can run the code for this demonstration, you must create an InfoPath form template that contains the fields in which to insert the data retrieved from Active Directory Domain Services.

To create a form to contain the user information

  1. In design mode, create a blank form template.

  2. On the Table menu, point to Insert, and then click Layout Table.

  3. Set Number of columns to 2, set Number of rows to 9, and then click OK.

  4. Drag nine Text Box controls from the Controls task pane into the cells in the second column of the layout table.

  5. Double-click each Text Box control, and rename the fields that are bound to the controls to the names in the following list.

    • FirstName

    • LastName

    • Alias

    • Email

    • Manager

    • Location

    • Title

    • TelephoneNumber

    • Department

  6. Type labels for each Text Box control in the first column of the layout table.

  7. Drag a Button control to a location below the nine text boxes, and then double-click the button. Change the Label to Get User Information, and then change the ID to GetUserInformation.

    The controls on your form template should look like the example in Figure 1.

    Figure 1. Text boxes and button for displaying user information
    Text boxes and button for displaying information

To allow form code to access user information in Active Directory Domain Services, the security level of the form must be set to Full Trust.

To set the form template to Full Trust

  1. On the Tools menu, click Form Options.

  2. In the Security and Trust category, clear the Automatically determine security level check box, and then click Full Trust.

  3. Click OK.

Note Note:

Setting the form to Full Trust is sufficient for previewing the form while you are testing it. When you deploy a form template that requires Full Trust, you must perform additional steps, such as digitally signing the form template or creating an installable form template. For information about how to do that, see "Deploying Form Templates That Require Full Trust" in How to: Deploy InfoPath Projects.

The following section describes how to add code to the button's event handler.

Adding the Code

To add user information to fields in the form's main data source, the code added to the Get User Information button's event handler performs the following operations:

For more information about using the XPathNavigator class to work in data in InfoPath form templates, see Working with XML Data Using the XPathNavigator Class in InfoPath 2007 Form Templates.

To add code to the Get User Information button

  1. Double-click the Get User Information button, and then click Edit Form Code to add code to the button's Clicked event handler. This opens the Visual Studio Tools for Applications development environment and adds an empty GetUserInformation_Clicked event handler.

    To use members of the System.DirectoryServices namespace in your form code, you must add a reference to its assembly in the Visual Studio Tools for Applications environment.

  2. On the Project menu, click Add Reference.

  3. On the .NET tab of the Add Reference dialog box, select System.DirectoryServices, and then click OK.

  4. At the top of the code module below the existing using statements, add the following statement

    C#
    using System.DirectoryServices;
  5. Locate the GetUserInformation_Clicked event handler and add the following code to it.

    C#
    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 FirstName = employee.Properties[
             "givenName"].Value.ToString();
          string LastName = employee.Properties["sn"].Value.ToString();
          string Mail = employee.Properties["mail"].Value.ToString();
          string Location = employee.Properties[
             "physicalDeliveryOfficeName"].Value.ToString();
          string Title = employee.Properties["title"].Value.ToString();
          string Phone = employee.Properties[
             "telephoneNumber"].Value.ToString();
          string Department = employee.Properties[
             "department"].Value.ToString();
    
          // The manager property returns a distinguished name, 
          // so get the substring of the common name following "CN=".
          string ManagerName = employee.Properties[
             "manager"].Value.ToString();
          ManagerName = ManagerName.Substring(
             3, ManagerName.IndexOf(",") - 3);
    
          // 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(FirstName);
          xnMyForm.SelectSingleNode("/my:myFields/my:LastName", ns)
             .SetValue(LastName);
          xnMyForm.SelectSingleNode("/my:myFields/my:Alias", ns)
             .SetValue(userName);
          xnMyForm.SelectSingleNode("/my:myFields/my:Email", ns)
             .SetValue(Mail);
          xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns)
             .SetValue(ManagerName);
          xnMyForm.SelectSingleNode("/my:myFields/my:Location", ns)
             .SetValue(Location);
          xnMyForm.SelectSingleNode("/my:myFields/my:Title", ns)
             .SetValue(Title);
          xnMyForm.SelectSingleNode(
             "/my:myFields/my:TelephoneNumber", ns)
             .SetValue(Phone);
          xnMyForm.SelectSingleNode("/my:myFields/my:Department", 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;
    }

    Visual Basic
    Try
       ' Get the user name of the current user.
       Dim userName As String = Me.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.
       Dim searcher As DirectorySearcher = New DirectorySearcher( _
          "(mailNickname=" + userName + ")")
    
       ' Search for the specified user.
       Dim result As SearchResult = searcher.FindOne()
    
       ' Make sure the user was found.
       If result Is Nothing Then
           MessageBox.Show("Error finding user: " + userName)
       Else
          ' Create a DirectoryEntry object to retrieve the collection
          ' of attributes (properties) for the user.
          Dim employee As DirectoryEnTry = result.GetDirectoryEnTry()
    
          ' Assign the specified properties to string variables.
          Dim FirstName As String = employee.Properties( _
             "givenName").Value.ToString()
          Dim LastName As String = employee.Properties( _
             "sn").Value.ToString()
          Dim Mail As String = employee.Properties( _
             "mail").Value.ToString()
          Dim Location As String = employee.Properties( _
             "physicalDeliveryOfficeName").Value.ToString()
          Dim Title As String = employee.Properties( _
             "title").Value.ToString()
          Dim Phone As String = employee.Properties( _
             "telephoneNumber").Value.ToString()
            Dim Department As String = employee.Properties( _
             "department").Value.ToString()
    
          ' The manager property returns a distinguished name, 
          ' so get the substring of the common name following "CN=".
          Dim ManagerName As String = employee.Properties( _
            "manager").Value.ToString()
          ManagerName = ManagerName.Substring( _
             3, ManagerName.IndexOf(",") - 3)
    
          ' Create an XPathNavigator to walk the main data source
          ' of the form.
          Dim xnMyForm As XPathNavigator = Me.CreateNavigator()
          Dim ns As XmlNamespaceManager = Me.NamespaceManager
    
          ' Set the fields in the form.
          xnMyForm.SelectSingleNode("/my:myFields/my:FirstName", ns) _
             .SetValue(FirstName)
          xnMyForm.SelectSingleNode("/my:myFields/my:LastName", ns) _
             .SetValue(LastName)
          xnMyForm.SelectSingleNode("/my:myFields/my:Alias", ns) _
             .SetValue(userName)
          xnMyForm.SelectSingleNode("/my:myFields/my:Email", ns) _
             .SetValue(Mail)
          xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns) _
             .SetValue(ManagerName)
          xnMyForm.SelectSingleNode("/my:myFields/my:Location", ns) _
             .SetValue(Location)
          xnMyForm.SelectSingleNode("/my:myFields/my:Title", ns) _
             .SetValue(Title)
          xnMyForm.SelectSingleNode("/my:myFields/my:TelephoneNumber", _
             ns).SetValue(Phone)
          xnMyForm.SelectSingleNode("/my:myFields/my:Department", ns) _
             .SetValue(Department)
    
          ' Clean up.
          xnMyForm = Nothing
          searcher.Dispose()
          result = Nothing
          employee.Close()
       End If
    
    Catch ex As Exception
       MessageBox.Show("The following error occurred: " + _
          ex.Message.ToString())
       Throw
    End Try

With the code in place, you can now preview the form to confirm that it works.

Important Important:

For the following code to work, you must be logged on to a network that uses Active Directory Domain Services to store user information.

To preview the form and test the code

  1. Switch back to Office InfoPath 2007.

  2. On the Standard toolbar, click Preview.

  3. To run the code, click Get User Information.

    The fields in the form should fill with your user information from Active Directory Domain Services.

  4. Click Close Preview.

Adding Code to the Loading Event Handler

In a more typical scenario, you want the user's information filled in automatically when the form is opened. To do that, you can copy the code from the button event handler to the Loading event handler of the form template.

To add the code to the Loading event handler

  1. On the Tools menu, point to Programming, and then click Loading Event.

  2. Copy the code from the GetUserInformation_Clicked event handler into the FormEvents_Loading event handler.

  3. Switch back to Office InfoPath 2007.

  4. On the Standard toolbar, click Preview.

    When the form opens, the fields in the form should automatically fill with your information from Active Directory Domain Services.

  5. Click Close Preview.

Adding Code to Enumerate the Available Attributes

To view all the attributes that are available for the current user in Active Directory Domain Services, you can add fields and code that display the names of the attributes and their values in the form.

To add fields to display attributes and their values

  1. Switch back to Office InfoPath 2007.

  2. On the View menu, click Data Source.

  3. In the Data Source pane, click Add a Field or Group.

  4. In the Add Field or Group dialog box, in the Name box, type gpProperties, set Type to Group, and then click OK.

  5. Right-click gpProperties, and then click Add.

  6. In the Name box, type gpProperty, set Type to Group, select the Repeating check box, and then click OK.

  7. Right-click gpProperty, and then click Add.

  8. In the Name box, type PropertyName, leave Type as Field (element) and Data type as Text (string), and then click OK.

  9. Right-click gpProperty, and then click Add.

  10. In the Name box, type PropertyValue, leave Type as Field (element) and Data type as Text (string), and then click OK.

  11. Drag gpProperty from the Data Source pane to a location below the Get User Information button.

  12. From the pop-up menu, select Repeating Table.

  13. Drag a Button control to a location below the Repeating Table, double-click the control, and then change the Label to List All Properties and change the ID to ListAllProperties.

    The controls added to your form template should look like the example in Figure 2.

    Figure 2. Text boxes and button for enumerating attributes
    Text boxes and button for enumerating atttributes

To display the enumerated attributes, add code to the button's Clicked event hander. The code for this event handler is similar to the code to set individual field values. However, instead of specifying which properties to retrieve, it loops through the entire PropertiesCollection object that is returned for the current user and displays each property's name in the text box bound to the PropertyName field and each property's value in the text box bound to PropertyValue field.

To add code to display attributes and their values

  1. Double-click the List All Properties button, and then click Edit Form Code to add code to the button's Clicked event handler. This opens the VSTA development environment, and adds an empty ListAllProperties_Clicked event handler.

  2. Add the following code to the ListAllProperties_Clicked event handler.

    C#
    // Get the user name of the current user.
    string userName = this.Application.User.UserName;
    
    // Create a DirectorySearcher object using the user name
    // as the search filter.
    DirectorySearcher searcher = new DirectorySearcher(
       "(mailNickname=" + userName + ")");
    
    // Create a DirectoryEntry object and retrieve the properties 
    // for the user.
    DirectoryEntry employee = searcher.FindOne().GetDirectoryEntry();
    
    // Create an XPathNavigator to walk the form's data source.
    XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
    XmlNamespaceManager ns = this.NamespaceManager;
    
    // Create XPathNavigators for setting values in
    // the properties table.
    XPathNavigator xnPropertiesTable = null;
    XPathNavigator xnPropertyName = null;
    XPathNavigator xnPropertyValue = null;
    
    // Loop through properties and display their values in
    // the properties table.
    foreach (string userProperty in employee.Properties.PropertyNames)
    {
        try
        {
            xnPropertiesTable = xnDoc.SelectSingleNode(
               "/my:myFields/my:gpProperties[last()]/my:gpProperty",
               ns);
            xnPropertyName = xnPropertiesTable.SelectSingleNode(
               "my:PropertyName", ns);
            xnPropertyValue = xnPropertiesTable.SelectSingleNode(
               "my:PropertyValue", ns);
            xnPropertyName.SetValue(userProperty);
            xnPropertyValue.SetValue(
               employee.Properties[userProperty].Value.ToString());
            xnPropertiesTable.InsertBefore(xnPropertiesTable.OuterXml);
        }
    
        catch (Exception)
        {
            xnPropertyValue.SetValue("Value cannot be displayed");
        }
    }
    
    // Clean up.
    searcher.Dispose();
    employee.Close();
    xnDoc = null;
    xnPropertiesTable = null;
    xnPropertyName = null;
    xnPropertyValue = null:

    Visual Basic
    ' Get the user name of the current user.
    Dim userName As String = Me.Application.User.UserName
    
    ' Create a DirectorySearcher object using the user name
    ' as the search filter.
    Dim searcher As DirectorySearcher = New DirectorySearcher( _
       "(mailNickname=" + userName + ")")
    
    ' Create a DirectoryEntry object and retrieve the properties 
    ' for the user.
    Dim employee As DirectoryEntry = searcher.FindOne().GetDirectoryEnTry()
    
    ' Create an XPathNavigator to walk the form's data source.
    Dim xnDoc As XPathNavigator = Me.MainDataSource.CreateNavigator()
    Dim ns As XmlNamespaceManager = Me.NamespaceManager
    
    ' Create XPathNavigators for setting values in
    ' the properties table.
    Dim xnPropertiesTable As XPathNavigator = Nothing
    Dim xnPropertyName As XPathNavigator = Nothing
    Dim xnPropertyValue As XPathNavigator = Nothing
    
    ' Loop through properties and display their values in
    ' the properties table.
    Dim userProperty As String
    For Each userProperty In employee.Properties.PropertyNames
        Try
            xnPropertiesTable = xnDoc.SelectSingleNode( _
               "/my:myFields/my:gpProperties[last()]/my:gpProperty", ns)
            xnPropertyName = xnPropertiesTable.SelectSingleNode( _
              "my:PropertyName", ns)
            xnPropertyValue = xnPropertiesTable.SelectSingleNode( _
               "my:PropertyValue", ns)
            xnPropertyName.SetValue(userProperty)
            xnPropertyValue.SetValue( _
               employee.Properties(userProperty).Value.ToString())
            xnPropertiesTable.InsertBefore(xnPropertiesTable.OuterXml)
        Catch
            xnPropertyValue.SetValue("Value cannot be displayed")
        End Try
    Next
    
    ' Clean up.
    searcher.Dispose()
    employee.Close()
    xnDoc = Nothing
    xnPropertiesTable = Nothing
    xnPropertyName = Nothing
    xnPropertyValue = Nothing

With the code in place, you can now preview the form to confirm that it works.

Important Important:

For this code to work, you must be logged on to a network that uses Active Directory Domain Services to store user information.

To preview the form and test the code

  1. Switch back to Office InfoPath 2007.

  2. On the Standard toolbar, click Preview.

  3. To run the code, click List All Properties.

    The left column lists the names of all the attributes that are available for the current user. The right column lists the values of the corresponding attribute in the left column.

    Note Note:

    Some of the values in the right column are not strings and cannot be displayed in a Text Box control. In that case, the type name of the value, such as System.Object[], System.__ComObject, or System.Byte[] is displayed instead.

  4. Click Close Preview.

Read It

This Office Visual How To demonstrates how to retrieve information about the current user from Active Directory Domain Services and automatically fill in fields with that information when an InfoPath 2007 form loads. The key steps for performing this process are as follows:

  1. Adding the Text Box controls and fields to display and store the user's information.

  2. Writing code that uses DirectorySearcher and DirectoryEntry classes of the System.DirectoryServices namespace to get user information from Active Directory Domain Services.

  3. Writing code that uses the XPathNavigator class of the System.Xml.XPath namespace to write those values to fields in the form's main data source.

This article also demonstrates how to enumerate the properties of the PropertyCollection object that is returned for the current user so that you can view the names of Active Directory attributes that are available.

Explore It

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
InfoPath Client      HyperGo   |   Edit   |   Show History
Hello,

I've re-created the solution and build it successfully. I've published the form successfully in my SharePoint Document Library, but when I click the new document, infopath prompts this message "InfoPath Cannot create a new, blank form. etc..." and the detail message is saying that "The form template is trying to access files and settings on your computer. InfoPath cannot grant access to these files and settings because the form template is not fully trusted. For a form to run with full trust, it must be installed or digitally signed with a certificate."

FYI, my form is not browser enabled. We're using the IP client.

Thanks,

Joel
Re:InfoPath Client      MarkRoberts   |   Edit   |   Show History
Joel,

This should be due to the trust level of the form, or perhaps how it was deployed. Confirm that that form template is set to Full Trust as described in the "To set the form template to Full Trust" steps in the article. If changing that setting and re-publishing the form does not correct the problem, see the information at "Deploying Form Templates That Require Full Trust" in How to: Deploy InfoPath Projects to make sure that the form template was published correctly to enable full trust.

Mark
Tags What's this?: Add a tag
Flag as ContentBug
Re:InfoPath Client      alaska_metal ... Bevan Stratton   |   Edit   |   Show History
I have gone through this example, and fallen at the first hurdle. Being new at this, I am not really sure where I have gone wrong. After adding the first Visual Basic code, and clicking the "Get User Information" button, I get an error message "Object reference not set to an instance of an object".

I have had to change three of the lines in the original code as follows, as that brought up errors too:

Dim

searcher As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher

Dim

result As DirectoryServices.SearchResult = searcher.FindOne()

Dim

employee As DirectoryServices.DirectoryEntry = result.GetDirectoryEntry()
Tags What's this?: Add a tag
Flag as ContentBug
example issues      Motten   |   Edit   |   Show History

The example above does have some issues; especially with its handling of null values in the DirectoryEntry object. I used the sAMAccountName and have posted by code below:


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(
"(sAMAccountName=" + 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 FirstName = employee.Properties["givenName"].Value.ToString();
string LastName = employee.Properties["sn"].Value.ToString();
string Mail = employee.Properties["mail"].Value.ToString();
string Location = string.Empty;

if (employee.Properties["physicalDeliveryOfficeName"].Value != null)
{
Location = employee.Properties["physicalDeliveryOfficeName"].Value.ToString();
}

string Title = string.Empty;
if (employee.Properties["title"].Value != null)
{
Title =employee.Properties["title"].Value.ToString();
}

string Phone = string.Empty;
if(employee.Properties["telephoneNumber"].Value != null)
{
Phone =employee.Properties["telephoneNumber"].Value.ToString();
}

string Department = string.Empty;
if(employee.Properties["department"].Value != null)
{
Department =employee.Properties["department"].Value.ToString();
}

// The manager property returns a distinguished name,
// so get the substring of the common name following "CN=".
string ManagerName = string.Empty;
if (employee.Properties["manager"].Value != null)
{
ManagerName = employee.Properties["manager"].Value.ToString();
ManagerName = ManagerName.Substring(
3, ManagerName.IndexOf(",") - 3);
}

//the rest is the same...

}
catch (Exception ex)
{
MessageBox.Show("The following error occurred: " + ex.Message.ToString());
throw;
}

Tags What's this?: Add a tag
Flag as ContentBug
Error in Sample C# Code      GabrielS ... Thomas Lee   |   Edit   |   Show History

In the section "To add code to display attributes and their values", the last line of the C# sample, in ListAllProperties_Clicked event has a typo. It ends with ":" but should end with ";"

Trouble with Null Values      bradall36   |   Edit   |   Show History

Hello, I've followed your instructions step by step but I'm still getting an error when I attempt to "Get User Information". When I preview the form and click on Get User Information, I receive the following error:

System.NullReferenceException
Object reference not set to an instance of an object.
at Template1.FormCode.GetUserInformation_Clicked(Object sender, ClickedEventArgs e)
at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

I have tried coding it in both C# and VB to see if that made a difference but it hasn't. I've also tried creating the form to be both browser-enabled and to use only Infopath. Both methods have drawn this same error. I would ideally like to be able to use browser-enabled forms to retrieve this data if possible? I'm using Infopath 2007 and will be deploying to a MOSS 2007 environment. Thank you very much for any help you can provide.

Brad

Tags What's this?: Add a tag
Flag as ContentBug
I'm getting the same error system.nullreferenceexception      Buck_Murdock   |   Edit   |   Show History

I'm getting the same error as bradall36. I run the debug on the VSTA and it takes me to the throw; line at the bottom. I'm not a programmer so i have no idea why i'm getting this error. i went through and followed the instructions step by step a couple of times to no avail. This would be so very useful to the form i'm creating, if anyone can help i would greatly appreciate it.

Thanks!!

Tags What's this?: Add a tag
Flag as ContentBug
I discovered the error with my form      Buck_Murdock ... Stanley Roark   |   Edit   |   Show History
It turns out that in AD I did not have a manager or a title listed in my user account. When i commented out the lines which i knew there was no information listed for in AD the form worked just fine. Unfortunately I don't know how to program, so i'm trying to figure out a way to return value like "N/A" or something for fields which have no information to import. Is there anyone who can help me with that? trying to figure out how to insert code into a pre-made code is hard, specially when you don't know what you're doing.
Flag as ContentBug
I'm getting the same error system.nullreferenceexception      MikeDavies   |   Edit   |   Show History
Another non-programmer getting this error. Please help !!

Thanks
Tags What's this?: Add a tag
Flag as ContentBug
Strange Active Directory search behavior (Authentication)      Mohammed Abed   |   Edit   |   Show History
I have infopath form template that loads current user's information from active directory as follows:

DirectorySearcher searcher = new DirectorySearcher("(sAMAccountName=" + this.Application.User.UserName + ")");
SearchResult searchResult = searcher.FindOne();
if (searchResult != null)
{
DirectoryEntry employee = searchResult.GetDirectoryEntry();
...

This form is used in web based SharePoint form library. when accessing this form (sharepoint site collection) from local machine it works fine, but when accessing it from external machine it fails.

the only way to make it work is by explicitly authenticate user name and password! like:
DirectorySearcher searcher = new DirectorySearcher(new DirectoryEntry("LDAP://Path", "Domain\\username", "password", AuthenticationTypes.Secure), "(sAMAccountName=" + displayName + ")");

I dont konw why it cants authenticate current logged in domain user when accessing from external machine.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker