2

I have a list of email adresses that needed to be added to a list using script. How can I resolve the email address to its ID, or set the User field directly with the email address?

This is my code (not working of course)

$(function (){

var link = _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/GetByTitle('Names')/items('2')";
var email = "[email protected]"
var data =  JSON.stringify({ '__metadata': { 'type': 'SP.Data.NamesListItem' }, 'User': 'i:0#.f|membership|'+email});

PostData(link, data);


function PostData(url, data){
    return $.ajax({
        url: url,
        method: "POST",
        data: data,         
        headers: 
        { 
            "accept": "application/json;odata=verbose",
            "content-type":"application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "MERGE",
            "If-Match": "*"
        },
    });
}
});

1 Answer 1

3

For getting the ID based on the user's email address, you can try to query like below -

https://<site>/_api/Web/lists/getbytitle('User Information List')/Items?$filter=EMail eq 'email id goes here'

After retrieving the ID, you can form your data object.

var itemData = {
   '__metadata': { "type": "SP.Data.NamesListItem" },
   "Title": 'New task',
   'UserId': userID   
};
1
  • Thanks, this works! One remark: the User Information list is at the site collection root site (which is fine)
    – JJD
    Aug 14, 2017 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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