none
How to add any user in SharePoint Information list(by email) using Rest API/ CSOM/SP Services / Jsom. RRS feed

  • Question

  • How to add any user in SharePoint Information list(by email).

    Note : I have user Email only, I don't have user account name. So using Email how can i add user in information list .

    As user is not added in site user in information list.


    Shiv Sharma


    Monday, October 14, 2019 11:10 AM

All replies

  • Hi Shiv,

    We can use /_api/web/ensureuser rest api to achieve it.

    If you have user email only, we can get the user account name in SharePoint 2013 using the code below.

    var email="test1@domain.com";	
    var accountName="i:0#.w|"+email.split('@')[1].split('.')[0]+"\\"+email.split('@')[0];

    The following example code for your reference.

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    	var email="test1@domain.com";	
        ensureUserByEmail(email);
    });  
    function ensureUserByEmail(email){
    	var accountName="i:0#.w|"+email.split('@')[1].split('.')[0]+"\\"+email.split('@')[0];
    	/// get the site url
    	var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
    	var payload = { 'logonName': accountName}; 
    	/// make an ajax call to get the site user
    	$.ajax({
    		url: siteUrl + "/_api/web/ensureuser",
    		method: "POST",
    		contentType: "application/json;odata=verbose",
    		data: JSON.stringify(payload),
    		headers: {
    			 "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    			 "accept": "application/json;odata=verbose"
    		},
    		success: function (data) {
    			alert(JSON.stringify(data));
    		},
    		error: function (data) {
    			alert(JSON.stringify(data));
    		}
    	});
    }
    </script>

    For SharePoint Online, we can get the account name by email using the code below.

    var email="dennis@tenant.onmicrosoft.com";
    var accountName="i:0%23.f|membership|"+email;


    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, October 15, 2019 2:30 AM
    Moderator
  • As mentioned Below by spiting email like this and make account name by email is not correct solutons as some users in the organization have different account name than email

    example user email is shiv.sharma@domain.com

    but his account name is domain\s.sharma

    So this is not correct solution 

    var accountName="i:0#.w|"+email.split('@')[1].split('.')[0]+"\\"+email.split('@')[0];

    Shiv Sharma

    Monday, October 21, 2019 4:39 AM
  • Hi Shiv,

    If the user not a site user, as a workaround, we need create a custom rest api and use server side code to get user account by email from Active Directory. Check thread below.

    lookup user in ActiveDirectory by email address

    More information:

    SharePoint 2013: Create a Custom WCF REST Service Hosted in SharePoint and Deployed in a WSP

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, October 21, 2019 9:53 AM
    Moderator
  • Can you suggest something in Client Side ?



    Shiv Sharma

    Thursday, October 31, 2019 9:46 AM
  • Hi Shiv,

    Unfortunately, SharePoint user profile REST API can't provide a way to check user by email from Active Directory(AD).

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, November 1, 2019 7:46 AM
    Moderator