0

I have a list with a multiple people column (name it Members).
I want to update item's column to add a user to it. But it have to keep all users already in the item's column. I want to do so using JavaScript.

So far, I have :

function addUser() {

    var clientContext = new SP.ClientContext(siteUrl);
    var list = clientContext.get_web().get_lists().getByTitle('MyList');
    var listItem= list.getItemById(listItemID);

    var newMembers = oldMembers + newUser; 
    listItem.set_item('Members',newMembers);
    listItem.update();

    clientContext.load(listItem);
    clientContext.executeQueryAsync(console.log("ok."), console.log("ko."));
}

newUser contains id and loginName like: 1#;i:0#.f|membership|nameUser@domain.com
In oldUser, i would like to have all user already in the field. To do so, i wanted to use this:

var oldMembers = listItem.get_item("Members");

But I keep having this error:

Uncaught Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Is there another way to add my new user at the end of my old users list?

Environment : SharePoint Online. I'm working on a display template for a search web part only for this list. Users should be able in one click to join an item.

0

It is not clear where in code you are executing this line:

var oldMembers = listItem.get_item("Members");

You should try to call the load function before that line:

clientContext.load(listItem, 'Include(Members)');

EDIT: You should also load the list after creating the list variable:

var list = clientContext.get_web().get_lists().getByTitle('MyList');
clientContext.load(list);
  • I was executing it just after getting listItem. – S61ze Feb 7 '17 at 14:47
  • I tried but it did'nt work either. – S61ze Feb 7 '17 at 14:51
  • Please see my updated answer. – Damjan Tomic Feb 7 '17 at 15:48
0

I managed to solve my problem using REST API.

First : Use Get to know whose users are already in the Members field

$.ajax({
    url: siteDirectoryUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$filter=Title eq '" + title + "'",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {

        //Action here
    },
    error: function (error) {
        console.log("second here: " + JSON.stringify(error));
    }
});

Second : Put the information in an array

var object = data.d.results;
var array = object[0].MembersId;

Third: Add current user to the array

array = object[0].MembersId.results;
array.push(userID); 

var dataInfos = {
    __metadata: {"type": "SP.Data.MyListListItem"},
    MembersId:{"results": array}
};

Fourth : Use Patch to update the Members field with the new user.

$.ajax({
    url: siteDirectoryUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + listItemID + ")",
    method:"PATCH",
    data:JSON.stringify(dataInfos),
    headers: {
        "accept": "application/json;odata=verbose",
        "content-Type": "application/json;odata=verbose",
        "X-Http-Method": "PATCH",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "If-Match": "*"
    },

    success: function (data) {},
    error: function (error) {
        console.log(JSON.stringify(error));
    }
});

I did it to remove user too. Feel free to ask me for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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