keesvanbemmel
keesvanbemmel

Reputation: 301

Adding domain user to Organisation Unit using Google Apps script

I'm trying to add a user (newly created) to a specific org unit in my Google Apps domain, but I can't seem to find any documentation or examples on this. Is this even possible? Perhaps through the use of plain REST calls?

My code so far to create a user:

var user = UserManager.createUser(userName, firstName, lastName, "welcome").setChangePasswordAtNextLogin(true);

Now I want to attach the user to a specific org unit, and make it a member of certain groups (but that's another question I'm diving into).

Any help will greatly be appreciated!

Regards,

Kees.

Upvotes: 0

Views: 2449

Answers (3)

Sergi Pérez
Sergi Pérez

Reputation: 1

In the same way that the last answer, you can add the organization unit on the user object when you create the user. In the user object you have to set the orgUnitPath property.

var user = {
    primaryEmail: "[email protected]",
    orgUnitPath:"/Students", 
        name: {
        givenName: "Albert",
        familyName: "Peretz"
    },
    password: "XWYlkf"
};
userGsuite = AdminDirectory.Users.insert(user);

Upvotes: 0

Enrique Umaran
Enrique Umaran

Reputation: 125

I think that this task is quite easy nowadays: This sinple code should work:

var emailAddress = '[email protected]';
var user = AdminDirectory.Users.get(emailAddress);
var orgunittomove='myorgunit'   //you can set the whole path;

user.orgUnitPath = orgunittomove;
AdminDirectory.Users.update(user, emailAddress);

Upvotes: 1

Claudio Cherubino
Claudio Cherubino

Reputation: 15014

The UserManager service doesn't support org units, but you can build the request manually.

The following Apps Script code adds an user to an OU. The parameters are the customerId, the email address of the user to add to the org unit and the org unit path:

function addUserToOU(customerId, email, ou) {
  var oauthConfig = UrlFetchApp.addOAuthService("google");

  var scope = "https://apps-apis.google.com/a/feeds/policies/";
  oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);         
  oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");

  oauthConfig.setConsumerKey("anonymous");
  oauthConfig.setConsumerSecret("anonymous");

  var body = "<?xml version=\"1.0\" encoding=\"utf-8\"?><atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:apps=\"http://schemas.google.com/apps/2006\"><apps:property name=\"orgUnitPath\" value=\"" + ou + "\" /></atom:entry>";

  var requestData = {
    "method": "put",
    "contentType": "application/atom+xml",
    "oAuthServiceName": "google",
    "oAuthUseToken": "always",
    "payload": body
  };

  var url = "https://apps-apis.google.com/a/feeds/orguser/2.0/" + customerId + "/" + email;
  var result = UrlFetchApp.fetch(url, requestData);
  Logger.log(result.getContentText());
}

Upvotes: 3

Related Questions