Question

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": "*"
        },
    });
}
});
Was it helpful?

Solution

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   
};
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top