Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get username inside a update plugin

Posted on by 435

Hi Folks , 

Please help me to find the username inside a update plugin.

Entity entity = (Entity)context.InputParameters["Target"];
Entity opp = new Entity("acc");
acc.Id = entity.Id;

acc.Attributes["new_loggedIn"] = context.UserId;

Thanks

*This post is locked for comments

  • windyMill Profile Picture
    windyMill 435 on at
    RE: How to get username inside a update plugin

    Thanks Goutam for making it clear.

    I didnt notice service was already in plugin code.

  • windyMill Profile Picture
    windyMill 435 on at
    RE: How to get username inside a update plugin

    Thanks , my mistake I didnt see it . Code is working perfectly

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to get username inside a update plugin

    This is OrganizationServiceProxy  -

    Here you go - 

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

    // Obtain the organization service reference.

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

    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get username inside a update plugin

    service is the instance of IOrganizationService. In followinf example it is shown how to get it - docs.microsoft.com/.../sample-create-basic-plugin

  • windyMill Profile Picture
    windyMill 435 on at
    RE: How to get username inside a update plugin

    Hi Andrew ,

    What is service here ?

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to get username inside a update plugin

    Hello,

    You can get user's id from context.UserId. The next step is to retrieve the name using endpoint:

    var username = service.Retrieve("systemuser", context.UserId, new ColumnSet("fullname")).GetAttributeValue<string>("fullname");

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