I have a sharepoint list, with a lookupfield for users. The column name is "SystemUser". I have successfully added a listitem by using javascript, setting the "SystemUser" to the current user by calling web.get_currentUser();
I want to set another user to the field "Users". I get the users list (web.get_siteUserInfoList()), and retrieve the user with a given ID. When the user is loaded, I set the loaded user as a value to the field "SystemUser".
But this approach does not work. I get the following errormessage (translated):
"An error occured: Invalid data is used to update the listelement. The field you tried to update could be writeprotected. Undefined";
Any ideas? Here's the code:
function () {
window.dialog = $(this);
var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('MyList');
var listItemCreationInformation = new SP.ListItemCreationInformation();
var listItem = list.addItem(listItemCreationInformation);
var name = $('#<%=txtbxName.ClientID %>').val();
var selectedUserID = $('#<%=hiddenUserFieldID.ClientID %>').val();
var userList = web.get_siteUserInfoList();
var selectedUser = userList.getItemById(selectedUserID);
context.load(selectedUser);
context.executeQueryAsync(function () {
listItem.set_item('SystemUser', selectedUser);
listItem.update();
context.executeQueryAsync(function () {
window.dialog.dialog('close');
SP.UI.Notify.addNotification('Added...');
},
function (sender, args) {
alert('Inner exception: ' + args.get_message() + '\n' + args.get_stackTrace());
});
}, function (sender, args) {
alert('Outer exception: ' + args.get_message() + '\n' + args.get_stackTrace());
});
}