Just wanting to know how to update a user field, like CreatedBy or Author based on a users name e.g. steven.martin? I seem to have it working fine, from a user id, but not from a user name.
get user fields by id
var context = new SP.ClientContext();
var user = context.get_web().getUserById(30); // e.g. user id = 30
context.load(user);
context.executeQueryAsync(
function () {
console.log(user.get_email());
var userField = user.get_id() + ";#" + user.get_loginName();
updateUser(userField);
},
function (sender, args) {
console.log('Error retrieving user properties');
}
);
update user/ author field
function updateUser(userField) {
var clientContext = new SP.ClientContext("/");
var list = clientContext.get_web().get_lists().getByTitle("Announcments");
var listItem = {};
listItem = list.addItem(new SP.ListItemCreationInformation());
listItem.set_item('Title', "My Announcement");
listItem.set_item("Author", userField);
listItem.update();
clientContext.executeQueryAsync(
function (sender, args) {
console.log('successfully updated');
},
function (sender, args) {
console.log('Request failed, adding new list item');
}
);
}