Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Creating records in sub grid with logged in user name when record is saved

Posted on by 195

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

  • bharrath Profile Picture
    bharrath 195 on at
    RE: Creating records in sub grid with logged in user name when record is saved

    Thanks Gowtham.

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Creating records in sub grid with logged in user name when record is saved

    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.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans