web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

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

(1) ShareShare
ReportReport
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!

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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;

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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/

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans