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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Gating Plug-In Execution by User

(0) ShareShare
ReportReport
Posted on by 215

Hey Everyone,


Right now I've created a plug-in that attaches to the Create and Update messages of Account and Contact. In this, we're assigning a field "jared_last_changed" that's used for our integration with our ERP. We have specific jobs that update the CRM from the outside, and they're all under a specific CRM UserName "misterclean". How would I check to see if the InitiatingUser's UserName is "misterclean"?

Any references that can lead me to an answer is greatly appreciated. Direct answers - doubly appreciated! Thanks in advance!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Hosk Profile Picture
    on at

    When a plugin is run it has UserId value in the plugin context

    localContext.PluginExecutionContext.InitiatingUserId

    There are two user values you can get two different user values, explanation taken from this blog post

    http://www.magnetismsolutions.co.nz/blog.aspx/zhenyu-wangs-blog/2012/07/07/the_difference_between_userid_and_initiatinguserid_in_plugin_of_dynamic_crm_2011

    • IExecutionContext.UserId Property: Gets the global unique identifier of the system user for whom the plug-in invokes Web service methods on behalf of. [1]

    • IExecutionContext.InitiatingUserId: Gets the global unique identifier of the system user account under which the current pipeline is executing. [2]

  • Suggested answer
    ScottDurow Profile Picture
    23 on at

    Hi,

    So you'll need to look at the plugin context Initiating User Id and do a query against the SystemUser entity by SystemUserId and return the fullname. msdn.microsoft.com/.../microsoft.xrm.sdk.iexecutioncontext.initiatinguserid.aspx

    I did a post about the initiating user id and the scenarios where it might not be what you expect under some scearios such as workflows updating records- www.develop1.net/.../User-Impersonation-in-Plugins-Workflow-and-Dialogs.aspx

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Jared,

    You can get the ID here:

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

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

       Guid userId = context.InitiatingUserId;

    With the ID you can get the username from query to system user, you can use query expression, simple entity retrieve by ID, linq or just follow my example here

    string strUserName = service.Retrieve("systemuser", context.InitiatingUserId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)).Attributes["domainname"].ToString();

    It will return the domain\username which you can compare to misterclean.

    Hope this helps!

    Thank you.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans