Home > Blockchain >  Using Google Admin API (C# / .Net)-- how can I add a first relationship to a user?
Using Google Admin API (C# / .Net)-- how can I add a first relationship to a user?

Time:11-18

I have working code that updates users names and supervisors with data from an HR SQL database. Everything works fine until I try to update the supervisor of a user who doesn't have a manager in Google. The object comes back with the UserToUpdate.Relations equal to null. It seems like I should be able to create a relation, but when I attempt to add the UserRelation item to the User, it errors with a "System.NullReferenceException: 'Object reference not set to an instance of an object.'" error. Any advice? Is there a way to add a relation to a user if they don't already have one?

code I'm running:

User UpdateUser = service.Users.Get(sUserEmail).Execute();
//code here to look up data in sql
UserRelation UR = new UserRelation();
UR.Type = "manager";
UR.Value = sManagersEmail;
UpdateUser.Relations.Add(UR);  //this is where it fails
service.Users.Update(UpdateUser, sUserEmail).Execute();

I've also tried using the .Insert method instead of .Add:

 UpdateUser.Relations.Insert(0,UR);  //this also fails

to no avail -- same result.

For users that already have a manager, this works:

UpdateUser.Relations[0].value = sManagersEmail; 

I can't find any further reference to do anything with .Net; I see how to do it with a JSON post outside .net's API, but it sure seems like I shouldn't have to do that...

Thank you!

CodePudding user response:

you should check if the relations property on the user object is not null, otherwise instantiate an empty list, to which you will add the new relationship object.

try to use this in place of the line of code that fails:

if(UpdateUser.Relations == null){
 UpdateUser.Relations = new List<UserRelation>();
}
UpdateUser.Relations.Add(UR);
  • Related