none
Retrieve User Email id by passing domain name using console application RRS feed

  • Question

  • Hi,

    Using console application i want to retrieve user email id by manually passing the user login name in sharepoint.

    I have tried 2 methods.
    Method -1:Retrieves user properties for the target user which i am passing in the code.(I want this method)

    Method-2 : Retrieves properties for current logged in user

    Method 1 shows empty for email  in the result.

    Please check the below method and let me know if any changes should be made in that.

       

     #region Retrieves the prop[erties for target user mentioned in the code
                // Replace the following placeholder values with the target SharePoint site and the
                // target user.
                const string serverUrl = "http://inbaghpc00250:33910/sites/CPMISC";
                const string targetUser = "techmahindra\\pn00351126";

                // Connect to the client context.
                ClientContext clientContext = new ClientContext(serverUrl);

                // Get the PeopleManager object.
                PeopleManager peopleManager = new PeopleManager(clientContext);

                // Retrieve specific properties by using the GetUserProfilePropertiesFor method. 
                // The returned collection contains only property values.
                string[] profilePropertyNames = new string[] { "PreferredName", "Department", "Title", "Email" };
                UserProfilePropertiesForUser profilePropertiesForUser = new UserProfilePropertiesForUser(
                    clientContext, targetUser, profilePropertyNames);
                IEnumerable<string> profilePropertyValues = peopleManager.GetUserProfilePropertiesFor(profilePropertiesForUser);

                // Load the request and run it on the server.
                clientContext.Load(profilePropertiesForUser);
                clientContext.ExecuteQuery();

                // Iterate through the property values.
                foreach (var value in profilePropertyValues)
                {
                    Console.Write(value + "\n");
                }
                Console.ReadKey(false);
                #endregion

           

        //#region Retrieves user properties for current logged in user
    //             //// String Variable to store the siteURL
    //            string siteURL = "http://inbaghpc00250:33910/sites/CPMISC";            

    //            //// To get the context of the SharePoint site to access the data
    //            ClientContext clientContext = new ClientContext(siteURL);

    //            //// PeopleManager class provides the methods for operations related to people
    //            PeopleManager peopleManager = new PeopleManager(clientContext);

    //            //// PersonProperties class is used to represent the user properties
    //            //// GetMyProperties method is used to get the current user's properties 
    //            PersonProperties personProperties = peopleManager.GetMyProperties();
    //            clientContext.Load(personProperties, p => p.AccountName, p => p.Email, p => p.DisplayName);
    //            clientContext.ExecuteQuery(); 
    //            //// Display the user  properties - AccountName, Email,  DisplayName
    //            Console.WriteLine("Account Name: " + personProperties.AccountName);
    //            Console.WriteLine("Email: " + personProperties.Email);
    //            Console.WriteLine("Display Name: " + personProperties.DisplayName);
    //            Console.ReadLine();
    //#endregion

    Regards,

    Poovi

    Friday, April 28, 2017 6:32 AM

Answers

  • Hi Poovi,

    The following code snippet for your reference:

    ClientContext ctx = new ClientContext("http://sp2013/sites/team");
    string targetUser = "denguo\\test";
    
    PeopleManager peopleManager = new PeopleManager(ctx);
    PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);
    ctx.Load(personProperties, p => p.AccountName, p => p.Email, p => p.DisplayName);
    ctx.ExecuteQuery();
    Console.WriteLine("Account Name: " + personProperties.AccountName);
    Console.WriteLine("Email: " + personProperties.Email);
    Console.WriteLine("Display Name: " + personProperties.DisplayName);
    Console.ReadLine();

    More information:

    How to: Retrieve user profile properties by using the .NET client object model in SharePoint 2013

    https://msdn.microsoft.com/en-us/library/office/jj163182.aspx

    Best Regards,

    Dennis


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

    Monday, May 1, 2017 5:49 AM
    Moderator
  • Hi,

    The issue resolved by running the exe file using the service account which is having full permission to farm and content db etc...

    Thanks

    Poovi

    • Marked as answer by Poovi Wednesday, May 10, 2017 6:00 AM
    Wednesday, May 10, 2017 6:00 AM

All replies

  • Hi Poovi,

    Go to User Profile Service on CA and check whether it is configured or not at first, then check the User Profile for the same user, whether you are having the Email ID in his User Profile Properties.

    If its not there then you would have to change the mappings for the same according to field that you have in your AD, with which your User Profile Sync Service is working.

    Alternatively you can also query the User Information List defined at the Site Collection Level to achieve the same results.

    --Rati


    Rati Ranjan

    Friday, April 28, 2017 7:49 AM
  • HI,

    Thanks for quick reply,

    I have checked the User profile properties for the user which i am checking in CA.

    It is configured and email is added for that user also.

    in second method the email is retrieved but it is for current user.I do not want for current user.

    Please suggest.

    Poovi

    Friday, April 28, 2017 8:55 AM
  • Hi Poovi,

    The following code snippet for your reference:

    ClientContext ctx = new ClientContext("http://sp2013/sites/team");
    string targetUser = "denguo\\test";
    
    PeopleManager peopleManager = new PeopleManager(ctx);
    PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);
    ctx.Load(personProperties, p => p.AccountName, p => p.Email, p => p.DisplayName);
    ctx.ExecuteQuery();
    Console.WriteLine("Account Name: " + personProperties.AccountName);
    Console.WriteLine("Email: " + personProperties.Email);
    Console.WriteLine("Display Name: " + personProperties.DisplayName);
    Console.ReadLine();

    More information:

    How to: Retrieve user profile properties by using the .NET client object model in SharePoint 2013

    https://msdn.microsoft.com/en-us/library/office/jj163182.aspx

    Best Regards,

    Dennis


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

    Monday, May 1, 2017 5:49 AM
    Moderator
  • Hi Poovi,

    Any update?

    If my reply helps you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best Regards,

    Dennis


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

    Monday, May 8, 2017 4:49 AM
    Moderator
  • Hi Dennis,

    Thanks for your reply and it worked.

    Actually i have tried using spuser properties using the below method .

                SPSite oSPsite = new SPSite("site url");

                SPWeb oSPWeb = oSPsite.OpenWeb();

                string strUser = "Domain\userid";

                SPUser myUser = oSPWeb.EnsureUser(strUser);

                string email = myUser.Email;

                string displayname = myUser.Name;

                Console.WriteLine("Email: " + email.ToString() + "\n" + "DisplayName: " + displayname.ToString());

                Console.ReadLine();

    The above code is showing below exception at SPSite oSPsite = new SPSite("site url"); line.

    The Web application could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

    i have tested below things.

    1)changed the platform to x64 bit

    2).net framework 4.5

    3)checked the url in browser and works fine

    4)have site collection admin access to my id

    5)added my id to wss_admin wpg group.

    Now i have no idea how to resolve this issue.

    Please help me to solve this

    Poovi

    Monday, May 8, 2017 6:35 AM
  • Hi Poovi,

    Please make sure the code run in SharePoint server and check if the url registered in the AAM settings.

    Best Regards,

    Dennis


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

    Monday, May 8, 2017 7:29 AM
    Moderator
  • Hi,

    The issue resolved by running the exe file using the service account which is having full permission to farm and content db etc...

    Thanks

    Poovi

    • Marked as answer by Poovi Wednesday, May 10, 2017 6:00 AM
    Wednesday, May 10, 2017 6:00 AM