How to get current user record in CRM plugin?

二次信任 提交于 2019-12-22 09:05:20

问题


I am developing a plugin . Whenever a plugin gets invoked, I need to get current user information ? Is there any way to retrieve that?


回答1:


The information is available in the PluginExecutionContext. The code below is from the Execute method your plugin must implement.

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    Guid userId = context.InitiatingUserId;
}

FYI, the context also has a "UserId" property that may or may not be the same as the InitiatingUserId. If your plugin step registration "Run in Users's Context" field has the value "Calling User", then they will be the same. If you have specified a user in the "Run in User's Context" field, then the UserId field will contain the user ID of the person you specify and the InitiatingUserId will be the actual CRM user whose action triggered the plugin. Sounds like you're looking for the InitiatingUserId.




回答2:


The above answer is correct. Also keep in mind that if you do not want to run the plugin in the user context you can also get the modifying user from the InputEntity that is passed into the plugin from whatever field is getting updated.

In the scenario where you are not running in user context you can then do a service.Retrieve call to get the full record for the modifying user.



来源:https://stackoverflow.com/questions/10109818/how-to-get-current-user-record-in-crm-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!