I'm trying to set a new user's Building id
using the .NET Google Admin SDK. I'm using the Locations
property to do that.
Here's Building id
in the Google Admin Console:
And in my code:
var newUser = new User()
{
// ...
};
// ...
newUser.Locations = new List<UserLocation>()
{
new()
{
BuildingId = command.BuildingId,
}
};
// ...
var request = _service.Users.Insert(newUser);
var user = await request.ExecuteAsync();
The last line throws Google.GoogleApiException
:
The service admin has thrown an exception. HttpStatusCode is BadRequest. Required parameter: [resource.location.field[0].Value]
I'm trying to set a new user's Building id
using the .NET Google Admin SDK. I'm using the Locations
property to do that.
Here's Building id
in the Google Admin Console:
And in my code:
var newUser = new User()
{
// ...
};
// ...
newUser.Locations = new List<UserLocation>()
{
new()
{
BuildingId = command.BuildingId,
}
};
// ...
var request = _service.Users.Insert(newUser);
var user = await request.ExecuteAsync();
The last line throws Google.GoogleApiException
:
Share Improve this question asked Mar 18 at 16:12 Eric EskildsenEric Eskildsen 4,8503 gold badges50 silver badges59 bronze badgesThe service admin has thrown an exception. HttpStatusCode is BadRequest. Required parameter: [resource.location.field[0].Value]
1 Answer
Reset to default 2tl;dr: Set Area and Type to desk
newUser.Locations = new List<UserLocation>()
{
new()
{
Area = "desk",
BuildingId = command.BuildingId,
Type = "desk",
}
};
Explanation
There are two issues with the code in my question:
The location's
Area
property is required. This doesn't seem to be documented.The below change eliminates the exception. However, the building ID doesn't show up in the GUI.
newUser.Locations = new List<UserLocation>() { new() { Area = "desk", BuildingId = command.BuildingId, } };
For the building ID to show up in the GUI,
Type
must also be set. Based on what the GUI setsType
andArea
to when you setBuilding id
there, it looks like both properties need to be set todesk
.With the change below, the building ID now shows up in the GUI under Employee info:
newUser.Locations = new List<UserLocation>() { new() { Area = "desk", BuildingId = command.BuildingId, Type = "desk", } };
Methodology
I discovered this by setting Building id
in the GUI, then getting the user through the API.
In the .NET Google Admin SDK, Locations
is, unfortunately, of type object
. At runtime, you can see it's a Newtonsoft array. I wrote an extension class that I hereby dedicate to the public domain:
using Google.Apis.Admin.Directory.directory_v1.Data;
using Newtonsoft.Json;
namespace Your.Namespace.Here;
public static class UserExtensions
{
public static List<UserLocation> GetLocations(this User user)
{
if (user.Locations is null)
{
return [];
}
return JsonConvert.DeserializeObject<List<UserLocation>>(user.Locations.ToString()!)!;
}
}