0
votes

I have the following Powershell CSOM code to add a user to a sharepoint online site.I am not trying to add him to any group, but to give him permission explicitly to the site.

However , I am getting the error : Exception calling "ExecuteQuery" with "0" argument(s): "Can not find the principal with id: 14."Can anyone tell me what I am doing wrong?

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server 
Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server 
Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL = "https://robinroyrbn.sharepoint.com/sites/AnotherTeamSite"
$UserAccount="i:0#.f|membership|[email protected]"

$PermissionToAdd="Read"


$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

$User = $Ctx.Web.SiteUsers.GetByLoginName($UserAccount)

$RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
$RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($User)

$RoleAssignment.RoleDefinitionBindings.Add($RoleDefToAdd)
$RoleAssignment.Update()
$Ctx.ExecuteQuery()

write-host  -f Green "User updated Successfully!"

}
Catch {
write-host -f Red "Error adding User !" $_.Exception.Message
}
1

1 Answers

0
votes

Sample tested script for your reference.

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL = "https://xxx.sharepoint.com/sites/leetest"
$UserAccount="[email protected]"

$PermissionToAdd="Read"

#region Variables 
$Username = "[email protected]" 
$Password = "password"
#endregion Variables

#$Cred = Get-Credential
#$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString $Password -AsPlainText -Force
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $securePassword)


$User = $Ctx.Web.EnsureUser($UserAccount)
$Ctx.Load($User)
$Ctx.ExecuteQuery()

$RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
#$RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($User)
#$RoleAssignment.RoleDefinitionBindings.Add($RoleDefToAdd)
#$RoleAssignment.Update()
$collRdb = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
$collRdb.Add($RoleDefToAdd)
$collRoleAssign = $Ctx.Web.RoleAssignments
$rollAssign = $collRoleAssign.Add($User, $collRdb)
$Ctx.ExecuteQuery()

write-host  -f Green "User updated Successfully!"

}
Catch {
write-host -f Red "Error adding User !" $_.Exception.Message
}