Please specify what version of the library you are using: [ 1.1.2 ]
Dear Team, what is the best way to add an new list item including a people field value.
In your documentation you provide this snippet:
sp.web.lists.getByTitle("PeopleFields").items.add({
Title: Util.getGUID(),
User1Id: 9, // allows a single user
User2Id: {
results: [ 16, 45 ] // allows multiple users
}
}).then(i => {
console.log(i);
});
As I undersand the User Id here e.g. 9 is the User id in the user hidden list.
My custom form uses a people picker generated by JavaScript.
How is it possible to retrieve the User-ID in case of a user that isn't listed in the user hidden list.
Many thanks for your support.
Hey @u48gmv,
Yep, userId is an Id from UIL. The best approach is to ensure a user. Please check the method sp.web.ensureUser. If a user is already in UIL then his/her Id is retrieved, otherwise, the user item is created in UIL and Id of the newly created item is returned.
Many thanks for the fast response. I will try it, and le you know.
We also have a section in the docs on updating a user field.
@patrick-rodgers thanks for the hint. i had a look at the docs first.
As far as I understand validateUpdateListItem needs the ID of the list item to update it.
So in order to insert the user value I would need to create a list item in first place, in order to get the ID and then update it.
This would cause two calls.
Somehow I can't get the ensureUser method to work (when using PnP JS) in our environment. I always get an empty result. If I use the ensureUser method with the standard SP.js then it works. Not sure why this happens.
So in our case I will use the standard SP.js methods to insert a new list item including a people field.
If you are interested I can post a code sample.
I somehow don't like the idea using ensureUser in oder to insert users in a people field.
In our case this would fill up the UIL unnecessarily.
I'm using the form for people to order a new site collection. So in our use case this users would never be part of the site collection where they request a site collection.
Thanks a lot for your support, and the fantastic work on pnp js.
If you have further hints, I'm happy to hear from you.
Maybe you should consider of a method to add people field values without using the ID of the user in the UIL.
P.S. sorry for the late reply
@u48gmv,
validateUpdateListItem expects JSON.stringify([{ Key: 'i:0#.f|membership|[email protected]' }]) for users.
There is a sister method addValidateUpdateItemUsingPath which allows creating items and uses the same payloads as validateUpdateListItem.
These guys should ensure users automatically on the backend.
It's very strange that the REST version of ensureUser didn't work for you while JSOM's one did. Maybe login name in ensure endpoint argument had a wrong format? Maybe it contained some German language specific symbols that should be encoded before sending in HTTP request?
@koltyakov
Thanks for the hint.
I will investigate in this.
Again, thanks for your fantastic work. It really helps a lot.
Closing the issue.
Most helpful comment
@koltyakov
Thanks for the hint.
I will investigate in this.
Again, thanks for your fantastic work. It really helps a lot.