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 = "My.Name@MyCompany.com"
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": "*"
        },
    });
}
});
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 '17 at 7:16

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.