问题
I already have a working function for adding user using Google Admin Directory:
string password = Random_Password();
User newuserbody = new User();
UserName newusername = new UserName();
UserOrganization newuserorg = new UserOrganization();
newusername.GivenName = GivenName;
newusername.FamilyName = FamilyName;
newuserbody.Name = newusername;
newuserorg.Department = Department;
newuserorg.CostCenter = Country;
newuserorg.Title = JobTitle;
newuserbody.Organizations = newuserorg;
newuserbody.PrimaryEmail = GivenName + "." + FamilyName + email_suffix;
//SET PASSWORD
newuserbody.Password = password;
service.Users.Insert(newuserbody).Execute();
This code works, but the details such as Department, Cost Center, and Title doesn't save on the record. Only First, Last name, email and password works.
Solved
Here's the code:
UserOrganization[] newuserorg = new UserOrganization[1];
newuserorg[0] = new UserOrganization();
newuserorg[0].Department = Department;
newuserorg[0].CostCenter = Country;
newuserorg[0].Title = JobTitle;
newuserbody.Organizations = newuserorg;
newuserbody.PrimaryEmail = GivenName + "." + FamilyName + email_suffix;
//SET PASSWORD
newuserbody.Password = password;
service.Users.Insert(newuserbody).Execute();
回答1:
you should try
List<UserOrganization> l = new List<UserOrganization> { newuserorg };
newuserbody.Organizations = l;
来源:https://stackoverflow.com/questions/30502824/google-admin-directory-api-add-user-employee-details