Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Wrong preselected user in dynamic form #1316

Conversation

SmarterJB
Copy link
Contributor

Q A
Bug fix? [x]
New feature? [ ]
New sample? [ ]
Related issues? fixes #1315

What's in this Pull Request?

I changed the way the DynamicForm component fetches the initial data from user fields. I changed it to use the UPN instead of the ID, because as described in issue #1315, this leads to problems when "People and Groups" are enabled in the SharePoint field.

I had to also remove the parsing of the user IDs from the defaultValues, because there are no longer the IDs there.

…turn the default value for the peoplepicker with the UPN and not the ID
@SmarterJB SmarterJB changed the title Bugfix/wrong preselected user in dynamic form Fix - Wrong preselected user in dynamic form Sep 19, 2022
@SmarterJB
Copy link
Contributor Author

@joelfmrodrigues maybe someone can look into this? we have a problem with that at a project from us, and this would really help a lot! :) thanks in advance!

@joelfmrodrigues
Copy link
Collaborator

@SmarterJB sorry that it took so long to find the time to review this.
I tested your code and everything seems to work well, except for editing an items with a user field containing a group.
Test list:
image
List item number 1 works fine, and the edit form displays the title and user (Adele) correctly by default.
I then tried with item number 2, and only the title is pre-populated. The group is not loaded.

I then switched back to the dev branch to double-check if this was a gap in your version compared to the current behaviour, and actually got the exact same result. The user is pre-loaded, but not the group.

Can you confirm if these were also the results that you experienced?

This was the part of your comment that made me double-check things:
"I had to also remove the parsing of the user IDs from the defaultValues, because there are no longer the IDs there"
Did we lose any functionality with your changes? (just to ensure I didn't miss something here)

@SmarterJB
Copy link
Contributor Author

Damn @joelfmrodrigues you are right, groups are not loaded correctly, because my select on fieldname/Name for users returns their UPN, but for Groups it just returns the groupname!

const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id,${fieldName}/Name&$expand=${fieldName}`;

Will look into this and get you a solution asap!

@SmarterJB
Copy link
Contributor Author

Hey @joelfmrodrigues! I figured it out, it was a problem we had in the code before, because in single user fields there was a request to the sharepoint rest API with /_api/web/getuserbyid(${userId}) which cannot work with a group as this will always yield a not found exception from the server!

I changed it now so that the value from the single field gets fetched the same way as the values from the multiuser field!

@SmarterJB
Copy link
Contributor Author

But I have no clue why the check failed now!

@joelfmrodrigues
Copy link
Collaborator

@SmarterJB Many thanks for the super quick update.
I have tested it using the same list but unfortunately, now the form never loads, for both user or group.
My list field is configured as a single value.

Went on debug and the issue seems to be in the foreach loop to parse the response items here:
image

The response I received was not an array:
image

And the following exception was captured
image

Could you please have a look?

@SmarterJB
Copy link
Contributor Author

Hey @joelfmrodrigues!
My bad! I converted a multiuser field to a singleuser field, thats why it still worked on my test!

I rewrote the single user method from before now it should work!

@joelfmrodrigues joelfmrodrigues merged commit 350646a into pnp:dev Oct 3, 2022
@joelfmrodrigues
Copy link
Collaborator

@SmarterJB many thanks! This is now merged and should be available on the beta release in a few minutes.

@joelfmrodrigues joelfmrodrigues added this to the 3.11.0 milestone Oct 3, 2022
AJIXuMuK pushed a commit that referenced this pull request Nov 16, 2022
* Removed parsing the user ID from default value, because the ID is no longer in the peoplePicker default value

* changed the 2 methods so that they do what the name intends - they return the default value for the peoplepicker with the UPN and not the ID

* Get Single User Field the same way as the multiple users

* rewrite getUserUPNById to getUserUPNFromFieldValue for single user values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants