Jul 12 2018
11:15 AM
- last edited on
Apr 04 2022
07:26 AM
by
TechCommunityAP
Jul 12 2018
11:15 AM
- last edited on
Apr 04 2022
07:26 AM
by
TechCommunityAP
We have an requirement to provide access to users on sharepoint online library without adding into any group, it will be direct lib access like share
I'm using below code but its not working for users,getting error Group does not exit. This code work if we want to add group instead users
#Load SharePoint CSOM Assemblies
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"
#Configuration Parameters
$SiteURL= "https://goodyearcorp.sharepoint.com/sites/NARetail/ResourceCentral/"
$ListName="District3172"
$GroupName="sharepoint@microsft.com" or users id
$PermissionLevel="Read"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the web and List
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($ListName)
#Break Permission inheritence - keep existing list permissions & Item level permissions
$List.BreakRoleInheritance($True,$True)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Permission inheritance broken..."
#Get the group or user
$Group =$Web.SiteGroups.GetByName($GroupName) #For User: $Web.EnsureUser('salaudeen@crescent.com')
$Ctx.load($Group)
$Ctx.ExecuteQuery()
#Grant permission to Group
#Get the role required
$Role = $web.RoleDefinitions.GetByName($PermissionLevel)
$RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
$RoleDB.Add($Role)
#Assign list permissions to the group
$Permissions = $List.RoleAssignments.Add($Group,$RoleDB)
$List.Update()
$Ctx.ExecuteQuery()
Write-Host "Added $PermissionLevel permission to $GroupName group in $ListName list. " -foregroundcolor Green
}
Catch {
write-host -f Red "Error Granting Permissions!" $_.Exception.Message
}