Getting value of people picker
-
02-10-2020 - |
Question
This is first time i use people picker. I have no experince with it. I hope that somebody can help me with getting value of people picker to save it in a new item as a person in my hosted list in SharePoint online.
This is my people picker code
function initializePeoplePicker(peoplePickerElementId) {
// Render and initialize the client-side People Picker.
// Create a schema to store picker properties, and set the properties.
var schema = {};
schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
schema['SearchPrincipalSource'] = 15;
schema['ResolvePrincipalSource'] = 15;
schema['AllowMultipleValues'] = true;
schema['MaximumEntitySuggestions'] = 50;
schema['Width'] = '151px';
this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);}
and this is the code when page starts
initializePeoplePicker('peoplePickerDiv');
and that's what I want. I want to write code instead of the question marks to send the user to the SharePoint list and att the person to person field
listItem.set_item("lfua", ????????????);
La solution
Please include following code:
// Query the picker for user information.
function getUserInfo() {
// Get the people picker object from the page.
var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
// Get information about all users.
var users = peoplePicker.GetAllUserInfo();
// Get the first user's ID by using the login name.
getUserId(users[0].Key);
}
// Get the user ID.
function getUserId(loginName) {
var context = new SP.ClientContext.get_current();
this.user = context.get_web().ensureUser(loginName);
context.load(this.user);
context.executeQueryAsync(
Function.createDelegate(null, ensureUserSuccess),
Function.createDelegate(null, onFail)
);
}
function ensureUserSuccess() {
var <YOURVAR> = this.user.get_id());
listItem.set_item("lfua", <YOURVAR>);
}
function onFail(sender, args) {
alert('Query failed. Error: ' + args.get_message());
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange