1

I have a situation where I need to get a user's id from their email address. I have a user email address which was already stored. Now the user changed his email address. By sending the old email address, how can I get the userid or can I get the new email address of the user by sending old email address?

The code that I used:

var customerid_d = $.Deferred();
var prefix = "i:0#.f|membership|";
var Email = prefix + Email;
var customerid_url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers(@v)?@v='" + encodeURIComponent(Email) + "'";
getData(customerid_url, customerid_d, false).done(function(data) {
    var result = data.d;
    if (result != undefined && result != "" && result != null) {
        id = result.Id;
    }
});

function getData(url, d, async) {
    $.ajax({
        url: url,
        method: "GET",
        async: async,
        headers: {
            "Accept": "application/json; odata=verbose"
        },
        success: function(data) {
            d.resolve(data);
        },
        error: OnError
    });
    return d.promise();
}

1 Answer 1

1

What you think was an email address, it's not really it. It's referring to the Active Directory property User Principal Name instead. If really that the user is changing their email address - not the User Principal Name - then your code will still work. But there's a catch.

Firstly before giving an answer, you need to understand how User Profile works in SharePoint. Inside SharePoint, you've got 2 'user information databases'. As per normal usage, any changes coming from the Active Directory will be synced to the SharePoint User Profile database. Do a check on your SharePoint Central Administration - User Profile, then find that user's profile. Verify whether the email address and account name are still the same or it was synchronized accordingly.

Find account in Central Administration - User Profile

Note: If the account name is changing, then the same user has been provisioned with a new user account completely.

Secondly, there is another 'user information database' that is stored within a hidden SharePoint List in each Site Collection separately, called 'User Information List'. Any query to the API _api/web/siteUsers will be done against this list directly. The reason why this list exists, mainly to achieve good performance.

You can go by this address (add /_catalogs/users/simple.aspx):

https://yoursharepointsitecolladdress.com/_catalogs/users/simple.aspx

When this list will be updated? When a user first time accessing your site collection, the user's information will be automatically added to this list. It's the same case when there is an update, the user must explicitly visit the site collection to get this list updated. If the user never in their life visited the Site Collection, your query likely fails. This legacy list is still there even nowadays with Office 365 world and still adopting the old mechanism. Read it on: https://zimmergren.net/sharepoints-hidden-user-list-user-information-list/.

Sometimes, this list is not getting updated instantly whenever there is a change and Microsoft acknowledges that. This is why we've got an API '/_api/web/ensureUser'.

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + '/_api/web/ensureUser',
    type: 'POST',
    contentType: 'application/json;odata=verbose',
    data: JSON.stringify({ 'logonName': 'i:0#.f|membership|[email protected]' }),
    headers: {
        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
        "accept": "application/json;odata=verbose"
    }
});

Read it on https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/jj245233(v=office.15). This API will force update the User Information List from SharePoint User Profile.

Another thing worth looking at, you could use another way to query based on other properties.

_spPageContextInfo.webAbsoluteUrl + "/_api/web/siteUsers?$filter=Title eq 'Sunil'"
_spPageContextInfo.webAbsoluteUrl + "/_api/web/siteUsers?$filter=LoginName eq '" + encodeURIComponent('i:0#.f|membership|[email protected]') + "'"
_spPageContextInfo.webAbsoluteUrl + "/_api/web/siteUsers?$filter=Email eq '" + encodeURIComponent('[email protected]') + "'"

Hope it helps. Other references:

1
  • Thank you so much for the information. I will go through the links you have shared. Commented Jan 25, 2021 at 13:48

Your Answer

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

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