dynamics-crmdynamics-crm-2011microsoft-dynamicsdynamics-crm-2013

getting a user's business unit on the basis of email address MS Dynamics


I have been able to get the currently logged-in user's business unit value on the basis of the system user id. I want to get the same business unit value but, on the basis of the "internalemailaddress" or simply the user's email address.

Till now, I have been able to get the current user's email address. But, don't know how to get the business unit value on the basis of it.

Is it possible to retrieve a user's business unit on the basis of the email address of the user?


Solution

  • You will need to fecth systemuser based on your internal email or user's email and then you will have to fetch businses unit based of systemuser found.

       <fetch top="50">
      <entity name="systemuser">
        <attribute name="internalemailaddress" />
        <attribute name="systemuserid" />
        <attribute name="fullname" />
        <filter>
          <condition attribute="internalemailaddress" operator="eq" value="abc@xyz.de" />
        </filter>
        <link-entity name="businessunit" from="businessunitid" to="businessunitid" link-type="inner">
          <attribute name="name" />
          <attribute name="businessunitid" />
        </link-entity>
      </entity>
    </fetch>
    

    Query expression

    // Define Condition Values
    var query_internalemailaddress = "abc@xyz.de";
    
    // Instantiate QueryExpression query
    var query = new QueryExpression("systemuser");
    query.TopCount = 50;
    
    // Add columns to query.ColumnSet
    query.ColumnSet.AddColumns("internalemailaddress", "systemuserid", "fullname");
    
    // Define filter query.Criteria
    query.Criteria.AddCondition("internalemailaddress", ConditionOperator.Equal, query_internalemailaddress);
    
    // Add link-entity query_businessunit
    var query_businessunit = query.AddLink("businessunit", "businessunitid", "businessunitid");
    
    // Add columns to query_businessunit.Columns
    query_businessunit.Columns.AddColumns("name", "businessunitid");
    

    OData Query

    https://CRMURL.crm.dynamics.com/api/data/v9.2/systemusers?$select=internalemailaddress,systemuserid,fullname&$expand=businessunitid($select=name,businessunitid)&$filter=(internalemailaddress eq 'abc%40xyz.de') and (businessunitid/businessunitid ne null)&$top=50
    

    SQL Query

    SELECT TOP 50 internalemailaddress, systemuserid, fullname, businessunit.name, businessunit.businessunitid
    FROM systemuser
    JOIN businessunit businessunit ON businessunit.businessunitid = systemuser.businessunitid
    WHERE internalemailaddress = 'abc@xyz.de'