Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get user email from plugin ?

Posted on by

I have made a plugin that works whenever a task is created in CRM. I need to get the email address of the person who has created the task but I am unable to do so. I have also created a post Image but with that I'm only able to get the userid form the person who created. Can someone please help with this

*This post is locked for comments

  • RE: How to get user email from plugin ?

    Entity objUser = Proxy.Retrieve("systemuser", userId, new ColumnSet("internalemailaddress", "systemuserid"));

                           if (objUser != null && objUser.Contains("internalemailaddress") && objUser["internalemailaddress"] != null)

                           {

                               string Useremail = (string)objUser["internalemailaddress"];

                               Console.WriteLine(Useremail);

                           }

  • Verified answer
    T.I.A Profile Picture
    T.I.A 1,760 on at
    RE: How to get user email from plugin ?

    public class Example : IPlugin

    {

    public void Execute(IServiceProvider serviceProvider)

    {

    IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

    IOrganizationService service = factory.CreateOrganizationService(pluginExecutionContext.UserId);

    Entity task = pluginExecutionContext.InputParameters["Target"] as Entity;

    Guid userId = pluginExecutionContext.UserId;

    Entity user = service.Retrieve("systemuser", userId, new ColumnSet(new String[] { "internalemailaddress" }));

    be sure do to null checking

  • T.I.A Profile Picture
    T.I.A 1,760 on at
    RE: How to get user email from plugin ?

    public class Example : IPlugin

    {

    public void Execute(IServiceProvider serviceProvider)

    {

    IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

    IOrganizationService service = factory.CreateOrganizationService(pluginExecutionContext.UserId);

    Entity task = pluginExecutionContext.InputParameters["Target"] as Entity;

    etc.

  • vagargg Profile Picture
    vagargg on at
    RE: How to get user email from plugin ?

    Do you need to install some other dependencies for this to work. I can't find a function named service

  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    Kalpavruksh D365 CoE 2,545 on at
    RE: How to get user email from plugin ?

    Hi,

    You will have to fetch user details from systemuser table by passing userid. In Create plugin, there is no need to get postimage as you will automatically get the ownerid of the record in context (which is userid). You can use service.retrieve method on systemuser table to get the email address of the user. 
     
    Entity objUser = service.Retrieve("systemuserid", userGuid, new ColumnSet("primaryemailaddress","systemuserid"));
    if (objUser != null && objUser.Contains("primaryemailaddress") && objUser["primaryemailaddress"] != null)
                {
                    string Useremail = (string)objUser["primaryemailaddress"];
                }
  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: How to get user email from plugin ?

    You have to retrieve email id from systemuser entity with help of ownerid of task.

  • T.I.A Profile Picture
    T.I.A 1,760 on at
    RE: How to get user email from plugin ?

    Correct

  • vagargg Profile Picture
    vagargg on at
    RE: How to get user email from plugin ?
    [deleted]
  • Suggested answer
    T.I.A Profile Picture
    T.I.A 1,760 on at
    RE: How to get user email from plugin ?

    Entity user = service.Retrieve("systemuser", userId, new ColumnSet(new String[] { "internalemailaddress" }));

    This will get the primary email address of the user.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans