Home

How to Share List or Library to a User using PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-215009%22%20slang%3D%22en-US%22%3EHow%20to%20Share%20List%20or%20Library%20to%20a%20User%20using%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-215009%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20have%20an%20requirement%20to%20provide%20access%20to%20users%20on%20sharepoint%20online%20library%20without%20adding%20into%20any%20group%2C%20it%20will%20be%20direct%20lib%20access%20like%20share%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20using%20below%20code%20but%20its%20not%20working%20for%20users%2Cgetting%20error%20Group%20does%20not%20exit.%20This%20code%20work%20if%20we%20want%20to%20add%20group%20instead%20users%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Load%20SharePoint%20CSOM%20Assemblies%3CBR%20%2F%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.dll%22%3CBR%20%2F%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll%22%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%23Configuration%20Parameters%3CBR%20%2F%3E%24SiteURL%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fgoodyearcorp.sharepoint.com%2Fsites%2FNARetail%2FResourceCentral%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgoodyearcorp.sharepoint.com%2Fsites%2FNARetail%2FResourceCentral%2F%3C%2FA%3E%22%3CBR%20%2F%3E%24ListName%3D%22District3172%22%3CBR%20%2F%3E%24GroupName%3D%22sharepoint%40microsft.com%22%20or%20users%20id%3CBR%20%2F%3E%24PermissionLevel%3D%22Read%22%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%23Setup%20Credentials%20to%20connect%3CBR%20%2F%3E%24Cred%20%3D%20Get-Credential%3CBR%20%2F%3E%24Cred%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24Cred.UserName%2C%24Cred.Password)%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3ETry%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Setup%20the%20context%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteURL)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.Credentials%20%3D%20%24Cred%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Get%20the%20web%20and%20List%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Web%3D%24Ctx.Web%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24List%3D%24web.Lists.GetByTitle(%24ListName)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Break%20Permission%20inheritence%20-%20keep%20existing%20list%20permissions%20%26amp%3B%20Item%20level%20permissions%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24List.BreakRoleInheritance(%24True%2C%24True)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.ExecuteQuery()%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BWrite-host%20-f%20Yellow%20%22Permission%20inheritance%20broken...%22%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Get%20the%20group%20or%20user%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Group%20%3D%24Web.SiteGroups.GetByName(%24GroupName)%20%23For%20User%3A%20%24Web.EnsureUser('salaudeen%40crescent.com')%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.load(%24Group)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.ExecuteQuery()%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Grant%20permission%20to%20Group%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Get%20the%20role%20required%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Role%20%3D%20%24web.RoleDefinitions.GetByName(%24PermissionLevel)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24RoleDB%20%3D%20New-Object%20Microsoft.SharePoint.Client.RoleDefinitionBindingCollection(%24Ctx)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24RoleDB.Add(%24Role)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Assign%20list%20permissions%20to%20the%20group%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Permissions%20%3D%20%24List.RoleAssignments.Add(%24Group%2C%24RoleDB)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24List.Update()%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.ExecuteQuery()%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BWrite-Host%20%22Added%20%24PermissionLevel%20permission%20to%20%24GroupName%20group%20in%20%24ListName%20list.%20%22%20-foregroundcolor%20Green%3CBR%20%2F%3E%7D%3CBR%20%2F%3ECatch%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bwrite-host%20-f%20Red%20%22Error%20Granting%20Permissions!%22%20%24_.Exception.Message%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-215009%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMicrosoft%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePpoint%20online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Kamal Pandey
Contributor

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
}