Get child Business Units based on a Parent Business Unit

Recently, I had to create a plugin that gets all the child Business Units of an specific Business Unit. As this can be a simple task for developers with experience in the customization of the platform, it can also be difficult to new developers. Hence, I wanted to share how I did it in case someone need it.

Code:

// Create XRM Service Context(Sending the IOrganization service as parameter)
XrmServiceContext XRM_Context = new XrmServiceContext(service);

// Parent Business Unit
EntityReference Parent_BusinessUnit = context.InputParameters["owningbusinessunit"] as EntityReference;

// LINQ query to get all child Business Units
List<Guid> Child_BusinessUnits = (from BusinessUnitEntity in XRM_Context.BusinessUnitSet
where BusinessUnitEntity.ParentBusinessUnitId.Id.Equals(Parent_BusinessUnit.Id)
select BusinessUnitEntity.Id).ToList();

/*
* Your logic with the list of child Business Units
*/

Hope it helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s