Set user in People picker control in SharePoint using JavaScript
$(document).ready(function() {
var url = "/_api/web/GetUserById(" + _spPageContextInfo.userId + ")/";
var user = "";
getItems(url, function(Users) {
user = Users.d.LoginName.split('|')[1];
if ($("select[title='Status'] option:selected").text() == "Received") {
$("select[title='Status']").val('Pending');
window.setTimeout(function() {
SetAndResolvePeoplePicker('Document Owner', user);
}, 750);
var d = new Date();
y = d.getFullYear();
m = d.getMonth() + 1;
day = d.getDate();
$("input[title='Reviewed Date']").val(m + "/" + day + "/" + y)
}
});
$("select[title='Status']").change(function(e) {
if ($(this).val() == "Approved"||$(this).val() == "Pending") {
var d = new Date();
y = d.getFullYear();
m = d.getMonth() + 1;
day = d.getDate();
$("input[title='Reviewed Date']").val(m + "/" + day + "/" + y)
} else {
$("input[title='Reviewed Date']").val("")
}
})
});
function SetAndResolvePeoplePicker(fieldName, userAccountName) {
var controlName = fieldName;
var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
peoplePickerEditor.val(userAccountName);
spPeoplePicker.AddUnresolvedUserFromEditor(true);
//disable the field
//spPeoplePicker.SetEnabledState(true);
//hide the delete/remove use image from the people picker
//$('.sp-peoplepicker-delImage').css('display','none');
}
var url = "/_api/web/GetUserById(" + _spPageContextInfo.userId + ")/";
var user = "";
getItems(url, function(Users) {
user = Users.d.LoginName.split('|')[1];
if ($("select[title='Status'] option:selected").text() == "Received") {
$("select[title='Status']").val('Pending');
window.setTimeout(function() {
SetAndResolvePeoplePicker('Document Owner', user);
}, 750);
var d = new Date();
y = d.getFullYear();
m = d.getMonth() + 1;
day = d.getDate();
$("input[title='Reviewed Date']").val(m + "/" + day + "/" + y)
}
});
$("select[title='Status']").change(function(e) {
if ($(this).val() == "Approved"||$(this).val() == "Pending") {
var d = new Date();
y = d.getFullYear();
m = d.getMonth() + 1;
day = d.getDate();
$("input[title='Reviewed Date']").val(m + "/" + day + "/" + y)
} else {
$("input[title='Reviewed Date']").val("")
}
})
});
function SetAndResolvePeoplePicker(fieldName, userAccountName) {
var controlName = fieldName;
var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
peoplePickerEditor.val(userAccountName);
spPeoplePicker.AddUnresolvedUserFromEditor(true);
//disable the field
//spPeoplePicker.SetEnabledState(true);
//hide the delete/remove use image from the people picker
//$('.sp-peoplepicker-delImage').css('display','none');
}
Comments