Anyone have any idea why this code does not work? - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Anyone have any idea why this code does not work?

Last post 05-18-2010 11:51 PM by Qazi Anis. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 05-18-2010 03:23 AM

    Anyone have any idea why this code does not work?

    I want to take the VALUE of a drop down down box (which is a list of users and the actual value is their domain username e.g. DOMAIN\username) pass it to a bit of code which searches Active directory and then outputs the users email address to a text box. I can do this for the logged on user no problem but the below code just does not work for the life of me, it is on the changed event of the drop down list so i know that the data is not read only. Any ideas?

     

    public void Authoriser_Changed(object sender, XmlEventArgs e)

    {

    // Ensure that the constraint you are enforcing is compatible

    // with the default value you set for this XML node.

     

    // Get the user name of the current user.

    XPathNavigator userName = this.CreateNavigator();string userName2 = userName.SelectSingleNode("/my:myFields/my:Authoriser", NamespaceManager).Value;

     

    userName2 = userName2(0, 6);

     

    // Create a DirectorySearcher object using the user name

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

    // use sam account name to search and extract more fields, in this case first name and last name

    DirectorySearcher searcher = new DirectorySearcher("(sAMAccountName=" + userName2 + ")");

    // Search for the specified user.

    SearchResult result = searcher.FindOne();

    // Make sure the user was found.

    if (result == null)

    {

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

    }

    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 Authoriser_email = employee.Properties["mail"].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:Authoriser_Email", ns)

    .SetValue(Authoriser_email);

     

     

    // Clean up.

    xnMyForm = null;

    searcher.Dispose();

    result =
    null;

    employee.Close();

    }

    }

  • 05-18-2010 03:43 AM In reply to

    Re: Anyone have any idea why this code does not work?

    what is this statement supposed to do 

    userName2 = userName2(0, 6);

    i think you should use

    userName2 = userName2.Substring(0, 6);

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-18-2010 03:49 AM In reply to

    Re: Anyone have any idea why this code does not work?

    its a mistake, i have put this line in now:

     

    string userName2 = userName.SelectSingleNode("/my:myFields/my:Authoriser", NamespaceManager).Value.ToString().Remove(0, 6);

     

    however still the same problem, output to a message box and the variable is being pulled ok but going wrong after that.

     

    Thinking about assigning the code to a button to see if it works better that way!!!

  • 05-18-2010 03:56 AM In reply to

    Re: Anyone have any idea why this code does not work?

    why dont you debug your code and see what is breaking

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-18-2010 04:18 AM In reply to

    Re: Anyone have any idea why this code does not work?

    Hi I have done this, its getting stuck on this line

     

    searchresult result = searcher.findone();

     

    the username is being passed ok e.g. an example username would be "tsy".

     

    its says "the "tsy" search filter is invalid" I dont understand how this can be invalid, it is the correct active directory username

     

  • 05-18-2010 06:07 AM In reply to

    Re: Anyone have any idea why this code does not work?

    try this.  

        DirectorySearcher search = new DirectorySearcher();
        search.Filter = String.Format("(SAMAccountName={0})", userName);
        search.PropertiesToLoad.Add("cn");
        SearchResult result = search.FindOne();

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-18-2010 06:52 AM In reply to

    Re: Anyone have any idea why this code does not work?

    Hi thanks for the reply. The updated code is below but still not working, getting frustrated now. I have tried putting in the line "LDAP:// "samaccount name +userName2 " (or along those lines)

     

    this is not working either.

     

    public void Authoriser_Changed(object sender, XmlEventArgs e)

    {

    {

    // Ensure that the constraint you are enforcing is compatible

    // with the default value you set for this XML node.

    // Get the user name of the current user.

    XPathNavigator userName = this.CreateNavigator();string userName2 = userName.SelectSingleNode("/my:myFields/my:Authoriser", NamespaceManager).Value.ToString().Remove(0, 6);

     

    DirectorySearcher search = new DirectorySearcher();

    search.Filter = String.Format("(SAMAccountName={0})", userName2);

    search.PropertiesToLoad.Add("cn");

    SearchResult result = search.FindOne();

     

     

     

    // Make sure the user was found.

    if (result == null)

    {

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

    }

    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 Authoriser_email = employee.Properties["mail"].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:Authoriser_Email", ns)

    .SetValue(Authoriser_email);

     

     

    // Clean up.

    xnMyForm = null;

    search.Dispose();

    result =
    null;

    employee.Close();

    }

    }

  • 05-18-2010 11:51 PM In reply to

    Re: Anyone have any idea why this code does not work?

    string[] domain = loginId.Split('\\');

    entry = new DirectoryEntry("LDAP://MyServer/DC=MyDomain,DC=net", "username", "password");

    Dsearch = new DirectorySearcher(entry);if (domain.Length == 2)

    {

    Dsearch.Filter =
    "(&(objectClass=user)(samAccountName=" + domain[1] + "))";

    }

    else

    {

    Dsearch.Filter =
    "(&(objectClass=user)(samAccountName=" + domain[0] + "))";

    }

    SearchResult sResultSet = Dsearch.FindOne();

    Qazi Anis
    Technical Architect
    Bitwise Inc
Page 1 of 1 (8 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.