Use AADConnect to Populate Office 365 Usage Location

So, a million years and tens of thousands of lines of code ago, I wrote a script for a customer to populate the Office 365 UsageLocation property (Set-MsolUser -UsageLocation) with the ISO country codes from Active Directory.  In Office 365, UsageLocation is used to determine what features are available to your users.

If you have the Country Code populated in Office 365, we can flow that value in to Azure AD.  But what if you *don't* have a country code set for everyone and just want it to be set to US if it's blank?

Here's a rule that will do just that.

  1. Launch the Synchronization Rules Editor.
  2. Under Rule Types, click Inbound, and then click Add New Rule.
  3. Under Connected System, select the connector that represents your Active Directory.
  4. Make the following selections:
    1. Connected System Object Type: user
    2. Metaverse Object Type: person
    3. Link Type: Join
    4. Precedence: <any value lower than the default rules>
  5. Click Next.
  6. On the Scoping Filter page, click Next.
  7. On the Join Rules page, click Next.
  8. On the Transformations page, do one of the following:
    1. If you want everyone to get the same usage location regardless of country code in Office 365, make the following selections:
      1. Flow Type: Constant
      2. Target Attribute: usageLocation
      3. Source: US
    2. If you want to only flow a certain value if the field is blank, make the following selections:
      1. Flow Type: Expression
      2. Target Attribute: usageLocation
      3. Source: IIF(IsNullOrEmpty([c]),"US",[c])
  9. Click Save.

Word to the wise: If you are populating AD with the country code value, make sure you are using the appropriate ISO 3166-2 country code.

Comments (10)

  1. Andrew Suthanah (O365Ltd) says:

    Hi there,
    This is great stuff. Is it possible to assign the country code depending on the users AD Domain? We have,, in the forest - ideally the AADConnect would be able to complete this by the domain. Can you share a method to do this assuming its possible?
    Many thanks

    1. Yes. You could create multiple rules (one per domain mapping), and then create a scoping filter on each rule. Inside the scoping filter, you would just create a single group with a single clause with DN contains dc=country,dc=domain,dc=com

      For example, if you had CN=John Smith,OU=Accounts,DC=uk,DC=company,DC=com and CN=Paul Clement,OU=Accounts,DC=france,DC=company,DC=com, you would create two rules (one for France, one for UK).

      Then, on the France rule, you would add a scoping filter that says DN CONTAINS dc=france,dc=company,dc=com
      On the UK rule, you would add a scoping filter that says DN CONTAINS dc=uk,dc=company,dc=com

      Since you're adding rules to a connector, you'll have to run a full sync to apply both of them.

      1. Andrew Suthanah (O365Ltd) says:

        Hi Aaron,

        Thanks for that useful info. I will have a go, you understood the situation perfectly so I am sure your method will work.


  2. Simon says:

    I would simply like to ensure that the country specified in our on premises AD country field flows into the O365 usage location field.
    What is the best way to do that?

    1. So, c -> c in inbound rules 104, 105, 106, and 107. UsageLocation is actually mapped from msExchUsageLocation in another rule. If you take the rule example I have and use a flow type of Constant and select the source attribute as c and target attribute as usageLocation, that should achieve your goal.

  3. PanicAttac says:

    Attempted the 2nd Option and it returns a error,

    The Expresion for the following mappings contain attributes that are note defined in the source schema:
    destination attribute : usagelocation
    Source Attribute(s): c,c

    " should I change the Source attribute "

    1. I just re-created it in my lab, and it still works. Make sure you copy/paste the expression into the Source column:


      It evaluates like this:
      If the value in [c] is null or empty, put the value "US" in. Otherwise, use the value present in [c]. It is case-sensitive, so if you have a capital C somewhere in there, you will generate that error.

  4. Alan Birch says:

    I get - Invalid character " encountered in expression IIF(IsNullOrEmpty([c]),”NZ”,[c]) at index 23 if I copy and paste the source expression. If I overtype the two " using my keyboard, I then get the 'contains attributes not defined in the source schema' warning. Safe to ignore?

    1. Is the Country code value something else in your AD environment? I'm not aware if the NZ locale causing any renaming of the attributes. You can check the AAD Connect schema to see if it's available.

Skip to main content