phpSPO
phpSPO copied to clipboard
how can assing role to new grop created?
hi, i need to know if is possible to assing a default roles (like owner, visitor or others) to a news group created by code
i have this code to make the news group,
$groupName = new GroupCreationInformation($name);
$newGroup = $this->client->getWeb()->getSiteGroups()->add($groupName);
$this->client->executeQuery();
and i use this code to assing role but its not found
$roleDef = $this->client->getWeb()->getRoleDefinitions()->getByName($name)->get()->executeQuery();
$idRole = $roleDef->getProperty('Id');
$group = $this->client->getWeb()->getSiteGroups()->getByName($name)->get()->executeQuery();
$idGroup = $group->getProperty('Id');
$assing = new SecurableObject($this->client);
$group = $assing->getRoleAssignments()->addRoleAssignment($idGroup, $idRole)->executeQuery();
Error
PHP Fatal error: Uncaught Office365RuntimeHttpRequestException: {"error":{"code":"-1,
Microsoft.SharePoint.Client.ResourceNotFoundException", "message":{"lang": "en-ES", "value": "Resource for request
roleAssignments is not found."}}}.
(I am new to gitlab and a beginner in English so I apologize if I don't understand or make any mistakes.)
thanks so much, greetings from Chile.
Roles are applied over lists:
$this->client->getWeb()->getLists()->getById($listId)->getRoleAssignments()->addRoleAssignment($idGroup,$idRole)->executeQuery();
hello @rfalcesSamca, thanks for the answer and help. I know that I can use your code to assign roles in a list or folder, but I need to assign roles to a new site group created by code. New custom group not as default group (owner - visitor). I can create a new site group but not assign roles to it.
Hello, @alfredoquintana In my tests, after creating a new custom group:
$groupName = new GroupCreationInformation($name);
$result= $this->client->getWeb()->getSiteGroups()->add($groupName)->executeQuery();
$newGroupInfo = $result->toJson();
Y can get the new id:
$newGroupId=$newGroupInfo['Id'];
And add it to the roleAssignments of an existing list:
$assign = $this->client->getWeb()->getLists()->getById($listIde)->getRoleAssignments()->addRoleAssignment($newGroupId,$idRole)->executeQuery();
$correctAsign=$assign!==null;
Hi @rfalcesSamca I apologize for not responding to your last comment. I have done your code and it works correctly so I thank you for the help provided.
In my case I only modified one thing getById() to getByTitle() to get the default library Shared Documents. I write it down in case it is useful to another user.
$assign = $this->client->getWeb()->getLists()->getByTitle(Shared Documents)->getRoleAssignments()
->addRoleAssignment($newGroupId,$idRole)->executeQuery();