User Usage Location - Microsoft Grapch C# .NET

Category: azure app service api apps public preview

Question

Htorrisi on Thu, 14 Mar 2019 05:22:38


Hey everyone,

This might be a really simple thing but I am stummped.

Using the Microsoft GraphServiceClient, I can add/remove licenses for users in office 365.

await GraphHandler.graphServiceClient.Users[userID].AssignLicense(licensesToAdd, licensesToRemove).Request().PostAsync();


Though a usageLocation always has to be set before a license can be assigned. I cant for the life of me find how to assign a usageLocation to a user before assigning the license.

Can anyone point me in the right direction? I have a feeling its not sported yet and Ill have to make POSTS to the restful API but I would prefer not to.


Replies

RyanHill-MSFT on Mon, 18 Mar 2019 19:16:20


Hi Htorrisi,

Thanks for the question. I'm currently investigating your issue and will reach back to you shortly.

Htorrisi on Tue, 19 Mar 2019 03:19:01


Hi Htorrisi,

Thanks for the question. I'm currently investigating your issue and will reach back to you shortly.


Thanks in advance, Ryan

Hey,

Thanks for looking into this. I have done a little more digging and found two references to UsageLocation.

The second one seams a bit more promising.

I found the object in a similar spot to Users under the GraphServiceClient. Though is does not seam to have the same sort of information as referenced in the above URL.

Could you please confirm if I am using the class correctly?

await GraphHandler.graphServiceClient.DirectoryObjects["SomeID"]....


RyanHill-MSFT on Tue, 19 Mar 2019 18:13:52


Try the following code snippets. You can set the location when creating the user or update location for an existing user. I used https://github.com/microsoftgraph/dotnetcore-console-sample as a guide.

public async Task CreateUser(string displayName, string alias, string domain, string password, string usageLocation)
{
    var userToAdd = BuildUserToAdd(displayName, alias, domain, password, usageLocation);
    await _graphServiceClient.Users.Request().AddAsync(userToAdd);
}

public async Task<User> FindByAlias(string alias)
{
    List<QueryOption> queryOptions = new List<QueryOption>
    {
        new QueryOption("$filter", $@"mailNickname eq '{alias}'")
    };

    var userResult = await _graphServiceClient.Users.Request(queryOptions).GetAsync();
    if (userResult.Count != 1) throw new ApplicationException($"Unable to find a user with the alias {alias}");
    return userResult[0];
}

public async Task UpdateUser(string alias)
{
    var userToUpdate = await FindByAlias(alias);
    userToUpdate.UsageLocation = "US";
    var result = await _graphServiceClient.Users[userToUpdate.Id].Request().UpdateAsync(userToUpdate);
}

public User BuildUserToAdd(string displayName, string alias, string domain, string password, string usageLocation)
{
    PasswordProfile passwordProfile = new PasswordProfile
    {
        Password = password,
        ForceChangePasswordNextSignIn = true
    };

    User user = new User
    {
        DisplayName = displayName,
        UserPrincipalName = $"{alias}@{domain}",
        MailNickname = alias,
        AccountEnabled = true,
        UsageLocation = usageLocation,
        PasswordProfile = passwordProfile
    };

    return user;
}

  

Htorrisi on Thu, 21 Mar 2019 04:44:27


Try the following code snippets. You can set the location when creating the user or update location for an existing user. I used https://github.com/microsoftgraph/dotnetcore-console-sample as a guide.

Thanks Ryan, you saved me from allot of manual work :)

Does seem to be a bit of an odd approach though seeing as if you want to assign information, you most likely already have the details of the user you want to update. So doing it by a search seams like extra work.

For example, I cant just take a user from the request builder, update then submit. I attempted to neaten it out by doing the following. Ended up with errors and is an invalid method.

var userResult = await GraphHandler.graphServiceClient.Users[userID].Request().GetAsync();
userResult.UsageLocation = "AU";
var result = await GraphHandler.graphServiceClient.Users[userID].Request().UpdateAsync(userResult);

But had to do it this way, following your post.

List<QueryOption> queryOptions = new List<QueryOption>
{ new QueryOption("$filter", $@"id eq '{userID}'") };
var usersResult = await GraphHandler.graphServiceClient.Users.Request(queryOptions).GetAsync();
var user = usersResult[0];
user.UsageLocation = "AU";
var result = await GraphHandler.graphServiceClient.Users[user.Id].Request().UpdateAsync(user);

Anyways, not complaining at all. The help has been much appreciated. :)


RyanHill-MSFT on Thu, 21 Mar 2019 16:11:53


I would agree but glad I could help!