How to Get User Business Unit Name by [UserId] Microsoft Dynamics CRM

  public string getBU(Guid userId)
        {
            string BU="";
            var service = new OrganizationService(connection);
                ColumnSet allFields = new ColumnSet() { AllColumns = true };
     try
            {  
                Entity team = service.Retrieve("team", userId, allFields);

                Entity user = service.Retrieve("systemuser", userId, allFields);
       
                Guid businessUnitId = ((EntityReference)user.Attributes["businessunitid"]).Id;

                //optional
                Entity businessunit = service.Retrieve("businessunit",                   businessUnitId, allFields);
                BU = businessunit["name"].ToString();
            }
            catch (Exception)
            {

              ;
            }
            return BU;

         }

Leave Comment

Your email address will not be published. Required fields are marked *