Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can i retrieve the systemusers parentbusinessunit name? in C#

(0) ShareShare
ReportReport
Posted on by

Im am trying to retrieve the systemusers parentbusinessunit id or name i c#

This is how i do it, but there must be a better,smarter way to do it:

Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
WhoAmIRequest who = new WhoAmIRequest();
WhoAmIResponse whoResp = (WhoAmIResponse)_service.Execute(who);
Guid currentUserId = whoResp.UserId;
string ownerid = currentUserId.ToString();
ColumnSet allFields = new ColumnSet() { AllColumns = true };
Entity user = _service.Retrieve("systemuser", currentUserId, allFields);
Guid businessUnitId = ((EntityReference)user.Attributes["businessunitid"]).Id;
Entity businessunit = _service.Retrieve("businessunit", businessUnitId, allFields);

Guid parentBusinessUnitId = ((EntityReference)businessunit.Attributes["parentbusinessunitid"]).Id;
Entity parentBU = _service.Retrieve("businessunit", parentBusinessUnitId, allFields);

Appreciate all the help!

thanks in advanced.

*This post is locked for comments

  • RE: How can i retrieve the systemusers parentbusinessunit name? in C#

    Thanks! exactly the code i needed.

  • Verified answer
    Nuno Profile Picture
    Nuno 2,005 on at
    RE: How can i retrieve the systemusers parentbusinessunit name? in C#

    Hi Gonzalo,

    You should not use AllCoumns equals to true unless you need to retrieve all the atributes from the entity.

    WhoAmIRequest whoAmIRequest = new WhoAmIRequest();
    WhoAmIResponse whoAmIResponse = xrm.Execute(whoAmIRequest) as WhoAmIResponse;
    
    Entity userBu = xrm.Retrieve("businessunit", whoAmIResponse.BusinessUnitId, new ColumnSet("parentbusinessunitid"));
    
    // IF USER HAS PARENT BUSINESS UNIT
    if (userBu.Attributes.Contains("parentbusinessunitid"))
    {
      Guid parentBuId = userBu.GetAttributeValue<EntityReference>("parentbusinessunitid").Id;
      String parentBuName = userBu.GetAttributeValue<EntityReference>("parentbusinessunitid").Name;
    }


  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: How can i retrieve the systemusers parentbusinessunit name? in C#

    So parentBU.GetAttributeValue<string>("name") should return you the name of parent business unit or even businessunit.GetAttributeValue<EntityReference>("parentbusinessunitid").Name

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans