hi friends,
I have one main form, it has subgrid. i want to get the logged in user name and want to create the record with same name in subgrid using plugin. please help me to achieve this using plugin.
Thanks,
Bharath
*This post is locked for comments
Thanks Gowtham.
Hi Bharath,
// get the context
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
// If you set "Run in User’s Context" is calling user then you will get logged in user from UserId property of context.
var userId = context.UserId;
// If you set "Run in User’s Context" is another specified user then you will get logged in user from InitiatingUserId property of context
var userId = context.InitiatingUserId;
//Once you get the I'd retrieve name of user or any other information.
var systemUser = service.Retrieve("systemuser", userId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
//In ColumnSet you can specify the columns which you want to retrieve (so for example "firstname", "lastname", "fullname" etc.).
//Now you can simply do
string username = systemUser["fullname"];
// You can create record using username.
Hope this helps.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156