Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Get the name of the initiating user by its ID using a plugin

Posted on by 45

Hello,

I have the following problem:

I need the name of the initiating user in a plugin. I get the execution context supplied with the plugin. I can easily find out the ID of the initiating user:

context.InitiatingUserId

During my research I came across this question and its answer on how to get the name by the ID:

Plugin to retrieve username

So I came up with this code trying to get the full name of the initiating user inside my OnExecute(context)-Function:

protected override void OnExecute(PluginContext context)
        {
            var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
            var systemUser = service.Retrieve("systemuser", context.InitiatingUserId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
            var initiatingUser = systemUser.ToEntity().FullName;
        }

Unfortunately, I keep getting the Error "The name 'serviceProvider' does not exist in the current context". 

What could be the cause of this error? Is there another way to find out the name of the initiating user?

I am grateful for any help!

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Get the name of the initiating user by its ID using a plugin

    Hi,

    I tried below code on my instance and it worked.

    public class FollowupPlugin: IPlugin
    {
    	public void Execute(IServiceProvider serviceProvider)
    	{
                //Extract the tracing service for use in debugging sandboxed plug-ins.
                ITracingService tracingService =(ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    	    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
    
                var systemUser = service.Retrieve("systemuser", context.InitiatingUserId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
                var initiatingUser = systemUser.ToEntity().FullName;
    	}
    }

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Get the name of the initiating user by its ID using a plugin

    Hello,

    Try to use the following:

    var systemUser = context.SystemUserService.Retrieve("systemuser", context.InitiatingUserId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

    var initiatingUser = systemUser.ToEntity<SystemUser>().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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans