Help Please: Active Directory - InfoPath Dev
in

InfoPath Dev

Help Please: Active Directory

Last post 03-02-2012 12:00 PM by stara. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 09-15-2009 01:28 AM

    Help Please: Active Directory

     I have had this problem for 3 days now. I entered this code but when I publish nothing happens not even the message box comes out. I really lost on wat is going on. when I use the this.Application.User.UserName; the user name comes out but if  I put the rest of the code it just goes dead.

    CODE:

    try
                {         
                string userName = this.Application.User.UserName;                    
                    DirectorySearcher adSearch = new DirectorySearcher(new DirectoryEntry("LDAP://*****/OU=*****,DC=*****,DC=****"));
                    adSearch.Filter = "(sAMAccountName=" + userName + ")";
                    SearchResult adSearchResult = adSearch.FindOne();
                    if (adSearchResult == null)
                    {
                        MessageBox.Show("Error finding user: " + userName);                              
                    }
                    else
                    {
                        DirectoryEntry employee = adSearchResult.GetDirectoryEntry();
                        string FirstName = (employee.Properties["displayName"].Value != null) ? employee.Properties["displayName"].Value.ToString():"";

                        XPathNavigator xnMyForm = this.CreateNavigator();
                        XmlNamespaceManager ns = this.NamespaceManager;
                        xnMyForm.SelectSingleNode("/my:myFields/my:Name", ns).SetValue(FirstName);
                        xnMyForm = null;
                        adSearch.Dispose();
                        adSearchResult = null;
                        employee.Close();
                    }
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show("The following error occurred: " +
                       ex.Message.ToString());
                   
                }         

  • 09-15-2009 10:59 PM In reply to

    Re: Help Please: Active Directory

    Hey Nash,

    For over five years people have been asking us to debug their Active Directory code. So, we developed an Active Directory Web Service: http://www.qdabra.com/en/products/ADWebService.aspx. It's cheap and comes with an hour of support.

    If you aren't a developer, I recommend trying our service. It has a lot of features and will save you time.

    If you are a developer, you should just debug your code... :)  We can't debug every specific line of code that goes into the forum. We are here to help people who have problems with InfoPath. In your case, it's a generic coding quesiton. Because we got so many forum posts that were the same, we developed the Active Directory Web Service.

    Sorry I can't be of further assistance.

     

    Patrick Halstead
    Project Manager at Qdabra
  • 09-16-2009 01:17 PM In reply to

    Re: Help Please: Active Directory

     

    Nash. I will assume that you are using a Browser Enabled form.  Additionally I will assume that you dont have access to the web services described in the earlier post.  I ran into a similar issue and here is the problem.

     

    MessageBox.Show is not a valid in the context you are using it.  In other words the form cannot display the message box.  It simply will crash.  Additionally removing the code probably left you with something like this example:

    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();

    {

    // 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 RequestorName = employee.Properties[

    "displayName"].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:Change_Requested_By", ns)

    .SetValue(RequestorName);

    // Clean up.

    xnMyForm = null;

    searcher.Dispose();

    result =
    null;

    employee.Close();

    }

     

    Which left you with another issue.

     If you leave this in: }
                catch (Exception ex)
                {

    Your form will still crash on load because of the unhandled exception.  Try taking the code I posted here and use it as a starting point.  You will have to modify it and Debug the last portion of the code in order to make your form work.  Post your results here and we can discuss it. 

     

    Regards,


     

  • 09-16-2009 11:04 PM In reply to

    Re: Help Please: Active Directory

    Thanks Steelman. You rock.

    Patrick Halstead
    Project Manager at Qdabra
  • 12-04-2009 06:38 AM In reply to

    Re: Help Please: Active Directory

     thanks got it

  • 08-30-2010 09:29 AM In reply to

    Re: Help Please: Active Directory

    I am publishing to a web browser form.  I can get the code to work in InfoPath, but not on the browser-enabled form.  It fills in the fields I need in InfoPath, but the fields are blank on the browser-enabled form.  I have searched everywhere on the web and cannot find an answer.  Please help.

  • 03-02-2012 12:00 PM In reply to

    • stara
    • Top 200 Contributor
    • Joined on 10-19-2010
    • Posts 54

    Re: Help Please: Active Directory

    Hi,

     

    Does this code search for user in different domain? If not how would I modify code to search user in a different domain?

     

    Thanks

Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.