Using COUNT to detect duplicate People Picker entries - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Using COUNT to detect duplicate People Picker entries

Last post 01-06-2017 03:24 PM by Hilary Stoupa. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 01-04-2017 12:29 PM

    Using COUNT to detect duplicate People Picker entries

    Considering the following structure: /my:myFields/my:participants/my:*/my:review/my:ppicker/pc:Person

    I'm trying to detect any duplicate people among my:participants.  So when I enter a name in any particular People Picker, I'd like to validate that it is unique among the set.
    I tried the Changing, Validating, and Changed events but they get fired twice; once for the Delete operation and once more for the Insert operation never showing the e.OldValue...

    my:paticipants is non-repeating.

     

    Josée Sévigny
  • 01-04-2017 12:52 PM In reply to

    Re: Using COUNT to detect duplicate People Picker entries

    Hiya - happy New Year! You can ensure your code only runs on insert by checking the Operation:

    if (e.Operation == XmlOperation.Insert)

     then proceed from there. You want the NewValue of the node, not the old value - but you may just want to run the code on the change of the AccountId, not the Person group (since when the fields change inside the Person group, I imagine events are firing on that group). Generally, I would just use a changed event - unless you want to throw a validation error if the user has already been selected for a different role?

    Hilary Stoupa

  • 01-04-2017 02:54 PM In reply to

    Re: Using COUNT to detect duplicate People Picker entries

     Hi Hilary,  Happy New Year to you too! ;-)

    So I wrote to following for 1 of the participant (which I'll extend to the rest of them once it works properly).  It works fine except that:

    1. I'm not seeing any error message/prompt.
    2. The People Picker is no longer editable.
    3. The previous value is gone (hence why I was after the e.OldValue) 


    public void leadEngPartner_review_ppicker_Person_AccountId_Changing(object sender, XmlChangingEventArgs e)
    {
     if (e.Operation == XmlOperation.Insert)
     {

      XPathNavigator xn = this.MainDataSource.CreateNavigator();
      XPathNodeIterator xni = xn.Select("//my:review[my:reviewRequired = 1 and (my:ppicker/pc:Person/pc:AccountId = '" + e.Site + "')]", NamespaceManager);
      
      if (xni.Count > 1)
      {
       e.CancelableArgs.Message = "This person is used more than once. All reviewers must be unique.";
       e.CancelableArgs.Cancel = true;
       return;
      }
     }
    }

    Josée Sévigny
  • 01-04-2017 03:00 PM In reply to

    Re: Using COUNT to detect duplicate People Picker entries

    This is on the changing event - I'd use the changed event, unless you want to instead throw a validation error (in which case you'd use the validating event...)? I guess I should clarify - what exactly do you want to have happen if the user selects someone who has already been selected? Set the people picker back to blank? Have a validation error? Something else? :)
    Hilary Stoupa

  • 01-06-2017 10:48 AM In reply to

    Re: Using COUNT to detect duplicate People Picker entries

    Hi,

    Sorry I had to jump on something else yesterday.

    I used the _Changing event as it's the only one of the 3 that has an e.CancelableArgs (which I think I need?!).

    Ideally, what I want is prevent duplicate participants, no matter what role they play in the process. I haven't been able to create a validation rule because I'm dealing with people pickers, so obviously some user feedback is desirable, which I could do through MessageBox.Show. So I rewrote my method as follows and it works ONCE.

    Sequence: I had Person A, B and C as participants. Change Person C for person A. fires the error and resets to C. Change for person B and error does not come up.

    string prevParticipantId = string.Empty;
    string prevParticipantName = string.Empty;


    public void participant_AccountId_Changed(object sender, XmlEventArgs e)
    {
     XPathNavigator xn = this.MainDataSource.CreateNavigator();
     switch (e.Operation)
     {
      case XmlOperation.Delete:
         prevParticipantId = e.Site.Value.Trim();
         prevParticipantName = e.Site.SelectSingleNode("../pc:DisplayName", NamespaceManager).Value.Trim();
         break;

      case XmlOperation.Insert:
         XPathNodeIterator xni = xn.Select("//my:review[my:reviewRequired = 1 and (my:ppicker/pc:Person/pc:AccountId = '" + e.Site.Value.Trim() + "')]", NamespaceManager);

         if (xni.Count > 1)
         {
            MessageBox.Show("This person is already in use. Please try again." );
            e.Site.SetValue(prevParticipantId);
            e.Site.SelectSingleNode("../pc:DisplayName", NamespaceManager).SetValue(prevParticipantName);
            e.Site.MoveToNext();
         }
         break;

      case XmlOperation.ValueChange:
       prevParticipantId = string.Empty;
       prevParticipantName = string.Empty;
       break;

      default:
       break;
     }
    }

     

     

    Josée Sévigny
  • 01-06-2017 02:20 PM In reply to

    Re: Using COUNT to detect duplicate People Picker entries

     Oh, I see - yah, if you just want to cancel their change if any exist already, then the changing event should be fine (and probably more straightforward than trying to set the people picker back to the original value). BUT what I think is happening is that the people picker is probably special - when the value changes, the control probably deletes the entire pc:Person node and adds a new one (thus no old value / new value). So - for your current approach, perhaps you need to also store / set the display name field, not just the account ID (whoops, I see now you are doing that .... ). Let me set up a quick repro and see if I have any other bright ideas. :)

    Hilary Stoupa

  • 01-06-2017 03:24 PM In reply to

    Re: Using COUNT to detect duplicate People Picker entries

     Sorry, I ran out of time to repro this - I'd suggest you run through the code in debug if you aren't already to see what's going on - I'm curious about why you are resetting the previous participant id and name to blank on the value change operation, and why you have e.Site.MoveToNext(); in the insert case? One thing that may also help is to drag the people picker fields onto the form as a repeating table & see what the values in the various fields are.

    Hilary Stoupa

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