1

I am getting various error messages depending on various attempted implementations to achieve the mentioned issue. Here is what I tried to do:

  1. Assign group on user creation

    //Get group object
    
    $group = $graph->createRequest('GET', "/groups/xxxxx-xxxxx-xxxx-xxxx-xxxxx")
          ->setReturnType(Model\Group::class)
          ->execute();
    
    $newUser = new Model\User();
    $newUser->setGivenName($firstName);
    $newUser->setSurname($surname);
    $newUser->setUserPrincipalName($userPrincipalName.'@xxxxxx.com');
    // $newUser->setUserType($userType);
    // $newUser->setMySite($website);
    $newUser->setPasswordProfile(["forceChangePasswordNextSignIn" => false,'password' => $pwd]);
    $newUser->setDisplayName($firstName.' '.$surname);
    $newUser->setMailNickname($firstName.$surname);
    $newUser->setMemberOf([$group]);
    $newUser->setAccountEnabled(true);
    
    $user = $graph->createRequest($action, "/users")
          ->attachBody($newUser)
          ->setReturnType(Model\User::class)
          ->execute();
    
  2. Assign membership through group object

    //we get $user object from the last line above     
    $grp = $graph->createRequest('POST', "/groups/xxxxx-xxx-xxxx-xxxx-xxxxxxx/members/\$ref")
          ->attachBody(["@odata.id" => $graph->$_baseUrl.'/'.$graph->$_apiVersion.'/users/'.$user->id])
          ->setReturnType(Model\Group::class)
          ->execute();
    

None of the above strategies work. I'd really appreciate pointers to where I am going wrong. Thank you in advance.

1
  • 1
    What are the "various error messages" you are getting? Commented Mar 9, 2018 at 15:09

1 Answer 1

1

I'm not sure if your first approach (referencing the group as part of memberOf when creating the user) is supported.

You second approach has a couple issues:

  1. By using $graph->$_baseUrl and $graph->$_apiVersion, you are attempting to reference private properties of $graph, which you cannot do (because they're private). See the PHP documentation for more information on visibility.
  2. You are attempting to reference the user's id attribute with $user->id. This would work only if $user had a public property named id (which it doesn't). Instead, you need to use the getter for the id: $user->getId().

An approach that works is the following (which is a slight variation of your second attempt):

// Add $user as a member of $group
$newMemberRef = 'https://graph.microsoft.com/v1.0/users/' . $user->getId();
$groupMembersRef = '/groups/' . $group->getId() . '/members/$ref';
$response = $graph->createRequest('POST', $groupMembersRef)
            ->attachBody(['@odata.id' => $newMemberRef])
            ->execute();

(Note how in this case we're referencing a user object, though you could also reference a group object, or (more generically) a directoryObject object.)

Sign up to request clarification or add additional context in comments.

1 Comment

All along I though the problem was with the SDK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.