Current User updated when opened later - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Current User updated when opened later

Last post 09-22-2009 02:00 PM by Hilary Stoupa. 11 replies.
Page 1 of 1 (12 items)
Sort Posts: Previous Next
  • 09-17-2009 09:27 AM

    • Sailor
    • Top 75 Contributor
    • Joined on 09-15-2009
    • Canada
    • Posts 107

    Current User updated when opened later

    Hello,

    I know I'm missing something VERY simple somewhere, but hey I'm a newbie.  Using some C# code to query AD to get current user info.  What method is required to ensure this isn't updated when the form is opened to view later?  We want to capture the data when the form is created then lock those fields.  It's got to be a rule or conditional formatting, but I'm just unsure.  

    Can someone help me with this?

     

  • 09-18-2009 03:39 PM In reply to

    Re: Current User updated when opened later

    I always just check to see if the field is blank. Sure, that won't protect you if something goes wrong when the form is first created, but that's what error handling is for... Don't put any controls on the form that the user can change the info with, just use expression boxes for display. That way another user can't clear something out.

    Hilary Stoupa

  • 09-21-2009 06:24 AM In reply to

    • Sailor
    • Top 75 Contributor
    • Joined on 09-15-2009
    • Canada
    • Posts 107

    Re: Current User updated when opened later

     Hello, and thank you for responding.  To be clear...an expression box will display the data pulled from the data source but won't store it?  I just want to understand the concept.  The fields in question are "tombstone" information pulled from a Sharepoint list based on the current AD User using C# code.  I don't want those fields updated after the first person opens the form.  I just create a rule?  I just don't understand where the rule goes and on fields.

  • 09-21-2009 09:04 AM In reply to

    Re: Current User updated when opened later

    I don't know what 'tombstone' information is? If you are getting information from a secondary data source and want to keep it with the form, you need to populate main data source fields with that information. Are you currently doing that?

    If so, I'd assume you have fields in your form's main data source like 'UserName' or 'UserAlias', etc. And that you would, using rules or code, fill those main data source fields when the form is first opened, is that what you are doing? All I'm suggesting is that if those main data source fields are not blank, you can assume they've already been populated (by the C# code you mention) and thus should not be overwritten the next time the saved xml is opened....

    Hilary Stoupa

  • 09-21-2009 09:33 AM In reply to

    • Sailor
    • Top 75 Contributor
    • Joined on 09-15-2009
    • Canada
    • Posts 107

    Re: Current User updated when opened later

     Hi,

     Sorry....tombstone data I'm referring to Name, Address, Telephone, etc.  And yes, my OnLoad event runs code to populate these fields on the main datasource.

    I guess I'm just really slow...what rule do I use on each of the fields I only want once?  Sorry if this is a simple question, I'm still new to form design.

  • 09-21-2009 09:58 AM In reply to

    Re: Current User updated when opened later

    If you are populating the fields in code, you'll want to address this in code. Before you populate each field (Name, Address, etc) simply check to see if the field is blank first in your code. So, I'm guessing since you say an OnLoad event, you are using the IP 2003 object model, and you'd want to see if the .text for each field was equal to "" prior to filling it.

    Hilary Stoupa

  • 09-21-2009 11:40 AM In reply to

    • Sailor
    • Top 75 Contributor
    • Joined on 09-15-2009
    • Canada
    • Posts 107

    Re: Current User updated when opened later

    Yes, I see now this would have to be done in the code.  Alas I am not a coder, but do get the concept of the conditions.  I was able to use the following code from a blog and make it work for my use.   I'm not usually one for hand holding, but in this case I need it.  Is anyone able to help me with the specific issue here?  I want to check one field (LastName) to see if it's blank, and continue if it is.  I just don't know in C# how to reference the Infopath field properly.

    I tried to add this:

     if ("/my:TrackChanges/my:GrpRequestor/my:LastName)" == null)
                {

                    // complete the rest of the lookup

                

     

    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:TrackChanges/my:GrpRequestor/my:FirstName", ns)
    .SetValue(FirstName);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns)
    .SetValue(LastName);
    //xnMyForm.SelectSingleNode("/my:myFields/my:Alias", ns)
    //.SetValue(userName);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Email", ns)
    .SetValue(Mail);
    //xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns)
    //.SetValue(ManagerName);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpLocation/my:Office", ns)
    .SetValue(Location);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Title", ns)
    .SetValue(Title);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Phone", 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;
    }
    }
  • 09-21-2009 12:08 PM In reply to

    Re: Current User updated when opened later

    Ah, okay, you are using the InfoPath 2007 Object Model.

    To check for blank, you can do something like: 

    // 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.
    //check to see if the first name field is blank
    XPathNavigator firstNameNav = xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:FirstName", ns);
    if (null != firstNameNav && firstNameNav.Value.Equals(""))
    {
          firstNameNav.SetValue(FirstName);
    }

    Now, it may be enough to simply check to see if one field is blank first, like the Last Name field, and if it is, set all your values. That may be the best route -- after all, you might have a user who doesn't have a phone number listed in AD, and you wouldn't want to then be filling it with another user's phone number simply because it was blank.

    In fact, I think given this scenario, I'd check for Last Name being blank at the beginning of my code, and not bother with creating the AD connection and all that if it wasn't.

    Hilary Stoupa

  • 09-21-2009 02:13 PM In reply to

    • Sailor
    • Top 75 Contributor
    • Joined on 09-15-2009
    • Canada
    • Posts 107

    Re: Current User updated when opened later

     God I must be thick in the head.  Like you said, yes I would like to check one field only to see if it's blank then proceed accordingly.  I understand this is a coding question moreso than Infopath and I'm greatful for the continued help.

     I tried it like this but it always pops up my "error" window even on the first time opening the form.

     

    XPathNavigator xnMyForm = this.CreateNavigator();
    XmlNamespaceManager ns = this.NamespaceManager;
    XPathNavigator LastNameNav = xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns);
    if (LastNameNav = null)
    {

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


    // Set the fields in the form.
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:FirstName", ns)
    .SetValue(FirstName);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:LastName", ns)
    .SetValue(LastName);
    //xnMyForm.SelectSingleNode("/my:myFields/my:Alias", ns)
    //.SetValue(userName);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Email", ns)
    .SetValue(Mail);
    //xnMyForm.SelectSingleNode("/my:myFields/my:Manager", ns)
    //.SetValue(ManagerName);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpLocation/my:Office", ns)
    .SetValue(Location);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Title", ns)
    .SetValue(Title);
    xnMyForm.SelectSingleNode("/my:TrackChanges/my:GrpRequestor/my:Phone", 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;
    }
    }
    else { MessageBox.Show("Error"); }
    }

  • 09-21-2009 02:53 PM In reply to

    Re: Current User updated when opened later

    Hi -- a single = sign is used to set a value. Basically, in your if statement, you are setting the value of LastName to null. Use '=='.

    Do you know how to set a break point and run your code in debug mode? Are you using Visual Studio or VSTA (comes with InfoPath)?

    Hilary Stoupa

  • 09-22-2009 12:29 PM In reply to

    • Sailor
    • Top 75 Contributor
    • Joined on 09-15-2009
    • Canada
    • Posts 107

    Re: Current User updated when opened later

     Yes, sorry i did have == I guess I deleted that accidently.  I can run the code without errors but it returns my

    else MessageBox.Show("Error")

    when I open the form and the LastNameNav is blank (Previewing the form).  I may have to abandon this cause I've spent hours trying to make this simple condition work (which is very important otherwise the originator data will be overwritten).

  • 09-22-2009 02:00 PM In reply to

    Re: Current User updated when opened later

    You don't have to have an 'else' statement. I had assumed you were using that for some kind of debugging? Just remove your 'else' that has the message box with the error. It seems to me that you have decent error handling in your if block -- you return an error if the user isn't in AD or if something goes awry in your try block....

    Hilary Stoupa

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