Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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());
        });
        }
share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Instead of writing the selectedUser (which I think is an SPUser object?) you should write a SPUserLookupValue, or it's string equivalent which is ID;#Name, i.e. 1;#David Lozzi.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.