none
unable getting Account name by Email of any other user using rest api SharePoint 2013 RRS feed

  • Question

  • unable  getting Account name by Email of any other user using rest api SharePoint 2013.

    Also , when i try with my email address its working and getting data. but its having issue with any other user case

    SiteURL/_api/Web/SiteUsers?$filter=Email eq '"+userEmail+"'

    Cannot read property 'LoginName' of undefined

    that is no data ....also hit url its not showing data its hwoing only

    /_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>5bfee2d3-d388-480f-b0a3-45cbde435d2d</id><title /><updated>2019-09-16T07:15:37Z</updated><author><name /></author></feed>

    here full function -

    function GetAccNameSpecialCab(IDFromUrl){
    debugger;
       $.ajax({
                      
                       url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUsers?$filter=Email eq '"+IDFromUrl+"'",
                       async: false,  
                       method: "GET",
                       headers: { "Accept": "application/json; odata=verbose" },
                       success: function (data) {
                                 debugger;
                       console.log(data);
                       var LoginNameSC=data.d.results[0].LoginName;
                       
                                if(data.d.results.length==0)
                                {
                                  alert("No user matched the Email");
                                }
                                if(data.d.results.length==1)
                                {
                                GetUserByEmail(LoginNameSC);
                                 //alert(data.d.results[0].LoginName);
                                }
                                                                                    
                      },
                      error: function (data) {
                          alert("Error: "+ data);
                     }
              });
    }


    Shiv Sharma





    Monday, September 16, 2019 7:38 AM

All replies

  • Hi Shiv Sharma,

    You need to use encodeURIComponent() 

    var strEmailaddress = encodeURIComponent("abc@gmail.com");

    Query Url should look like

    _spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUsers?$filter=Email eq '"+strEmailaddress+"'"

    Best Regards,

    Karthick Rajan




    Monday, September 16, 2019 8:09 AM
  • Hi Karthick,

    I did , but still facing same issue.


    Shiv Sharma

    Monday, September 16, 2019 8:21 AM
  • Also , when i try with my email address its working and getting data. but its having issue with any other user case 

    Shiv Sharma

    Monday, September 16, 2019 8:23 AM
  • It Should work for all users when its working for your Email address. Can you share the sample JSON URL for non working user
    Monday, September 16, 2019 8:37 AM
  • _api/Web/SiteUsers?$filter=Email%20eq%20%27FirstName.LastName@Domain.com%27

    Shiv Sharma

    Monday, September 16, 2019 9:08 AM
  • Getting this 

    <?xml version="1.0" encoding="utf-8"?><feed xml:base="http://DoaminName/sites/SiteName/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>aa8821a5-334e-49af-838a-29aaf15ce2e4</id><title /><updated>2019-09-16T09:07:53Z</updated><author><name /></author></feed>


    Shiv Sharma

    Monday, September 16, 2019 9:10 AM
  • Hi Shiv Sharma,

    Please refer the below code,

    function getbyEmail()
    {
       var Emailaddress = encodeURIComponent("karthick.rajan@domain.com");
       $.ajax({                  
           url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUsers?$filter=Email eq '"+Emailaddress+"'",
           async: false,  
           method: "GET",
           headers: { "Accept": "application/json; odata=verbose" },
           success: function (data) {       
           		console.log(data);                                                                               
              },
          error: function (data) {
              alert("Error: "+ data);
         }
      });
    }

    Here is the JSON URL,

    http://domain/en-us/_api/Web/SiteUsers?$filter=Email eq 'karthick.rajan%40domain.com'

    After encodeURIComponent, @ in the email address is converted into %40 in this case. In your URL, it was missing ( _api/Web/SiteUsers?$filter=Email%20eq%20%27FirstName.LastName@Domain.com%27 )

    Please encode the Email address before passing it to JSON URL

    Karthick Rajan



    Monday, September 16, 2019 9:25 AM
  • Try using your endpoint in below format:

    siteUrl/_api/web/siteusers/getbyemail('userEmailAddress')

    -------------------------------------------------------------------------------------------------

    Please Vote(^) and accept as an Answer if it helped you.

    Monday, September 16, 2019 9:25 AM
  • http://Doamin/sites/SiteCollection//_api/Web/SiteUsers?$filter=Email%20eq%20%27shiv.sharma%40india.nec.com%27

    Getting this :

    <?xml version="1.0" encoding="utf-8"?><feed xml:base="http://Domain/sites/Site/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>dc2cac61-14f2-4526-a10e-9e5030ebc57c</id><title /><updated>2019-09-16T09:29:12Z</updated><author><name /></author></feed>


    Shiv Sharma

    Monday, September 16, 2019 9:32 AM
  • SP.UserCollection object contains the SP.UserCollection.getByEmail Method to get the user with the specified email address.

    http://Domain/sites/sitecollection/_api/web/SiteUsers/getByEmail('shiv.sharma@india.nec.com')

    Regards,

    • Proposed as answer by Rajan Karthick Monday, September 16, 2019 10:09 AM
    • Unproposed as answer by Rajan Karthick Monday, September 16, 2019 10:09 AM
    Monday, September 16, 2019 9:40 AM
  • Hi Shiv,

    Get Accout Name by checking email like this:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
                var email="Jerry@Contoso.com";
                $.ajax({
                      
                       url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUsers/getByEmail('"+ email +"')",
                       method: "GET",
                       headers: { "Accept": "application/json; odata=verbose" },
                       success: function (data) {
                                console.log(data);
                                if(data.d !=null)
                                {
                                  alert(data.d.LoginName);
                                }
                                else
                                {
                                   alert("No user matched the Email");
                                }
                                                                                      
                      },
                      error: function (data) {
                          alert("Error: "+ data);
                     }
              });
    
    </script>
    

    Thanks

    Best Regards


    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, September 17, 2019 7:40 AM
  • Hi Jerry,

    Getting Following Error

    <m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code>-2146232832, Microsoft.SharePoint.SPException</m:code>
    <m:message xml:lang="en-US">User cannot be found.</m:message>

    </m:error>

    But i checked in SharePoint User Profile Properties, Its showing in user profile properties with same email id . 

    I think this user never visit this  site that is why its not known by this site , that is why getting following error for this user .

    Is there any solution for this???

     


    Shiv Sharma

    Wednesday, September 18, 2019 4:32 AM
  • Because when i added this user in this site share point group its able to fetch data  by your end point

    Shiv Sharma

    Wednesday, September 18, 2019 4:33 AM
  • But this is not the solution for every user? in case that user is never visit the site! Is there any solution for this ??

    Shiv Sharma

    Wednesday, September 18, 2019 4:34 AM
  • /SiteUsers endpoint gets data from the "User Information List" which is a hidden list on the Site. If user does not have any permission on this site or has NEVER visited the site will not be able to access the /SiteUsers endpoint.

    If user has visited the site or had a permission on the site and then removed, you can try "Ensure User" and then calling the above rest endpoint.

    Check this link. It might help you.

    https://sharepoint.stackexchange.com/questions/233636/api-web-siteusers-does-not-return-user-until-i-check-users-permissions


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


    • Edited by HarshalGite Wednesday, September 18, 2019 5:26 AM
    Wednesday, September 18, 2019 5:10 AM
  • Hi shiv,

    /_api/web/SiteUsers will query from User Information List, this list will need user have permission and have visited the site.

    In your side, if a user have never visited the site, it won't able to find the user. This is the limitation.

    If query the User Propties with _api/SP.UserProfiles.PeopleManager/GetPropertiesFor, this is not supported to query user by email, please check the similiar question below:

    Get user properties by email from SharePoint 2013 User Profile

    Currently, based on the limiation above, it's necessary to add the users to a SharePoint site group and then use Rest API above if want to get user account name by email.

    Thanks

    Best Regards


    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, September 20, 2019 8:04 AM