SharePoint Permission Tasks Using PnP PowerShell
Softree Technology > Blogs > PnP PowerShell > SharePoint Permission Tasks Using PnP PowerShell

SharePoint Permission Tasks Using PnP PowerShell

  • Posted by: Softree Technology

In this blog, we are going to discuss about the SharePoint permission in different scenarios like how to assign & remove permission to a user, how to assign unique permission. Also, we will retrieve the users and the corresponding role associated with them.

SharePoint Permission is used for assigning different types of roles/permission to different users. A user can perform activity according to the permission or role which is assigned to the user. There are Different types of permissions/roles available in SharePoint.

Here, we will use PnP Online to perform the permission tasks. Please follow the below Code snippet and the steps given to perform the permission tasks.

Step-1: – Let us open the windows PowerShell ISE as administrator, and then run the code in PowerShell.

Step-2: – Enter the user Credentials.

Step-3: – Get Site collection, Group & User.

Step-4: – Get the user associated with site and their respective role.

Step-5: –Then assigned permission to a user & also mentioned how to remove the permission.

Step-6: – Also assigned unique permissions for list, list items & subsite. For subsite we have to assign the unique permission while creating the subsite by breaking the root site inheritance.

# Provide URL of the Site 
$sUrl = "https://Contoso.sharepoint.com/sites/SiteName"
try
{
#pass valid credentials 
Connect-PnPOnline -Url $sUrl -Credentials (Get-Credential)
$siteColl = Get-PnPTenantSite

Get-PnPGroup 

Get-PnPUser 

#To get the user and their corresponding role
$web = Get-PnPWeb -Includes RoleAssignments
foreach($ra in $web.RoleAssignments)
{
$member = $ra.Member
 $loginName = get-pnpproperty -ClientObject $member -Property    LoginName
$rolebindings = get-pnpproperty -ClientObject $ra -Property RoleDefinitionBindings
write-host "$($loginName) - $($rolebindings.Name)"
write-host  
}
#To assign a role/Permission to the user in site
Set-PnPWebPermission -User 'test@user.onmicrosoft.com' -AddRole 'contribute'
#To remove permission of an user
Set-PnPWebPermission -User 'test@user.onmicrosoft.com' -RemoveRole 'Read'
# Add unique permission to list, first we have remove unique permissions if any
Set-PnPListPermission -Identity 'ctlist' -User 'test@user.onmicrosoft.com' -AddRole 'Contribute'
# Add unique permission to list items
Set-PnPListItemPermission -List 'MULlist' -Identity 3 -User 'test@user.onmicrosoft.com' -AddRole 'Edit'

write-host "Successful"

$mysubsite = New-PnPWeb -Title "Subsite" -Url  Subsiteurl 
-Description "A subsite" -Locale 1033 -Template "STS#0" -BreakInheritance 

Add-PnPTenantSequenceSubSite -Site $siteColl -SubSite $mysubsite


Write-host "Site '$SiteTitle' Created Successfully!" 
}
catch
{
 write-host -f Red "Error:" $_.Exception.Message
} 

Conclusion: Hence we have concluded after performing the above operation that we can set/recognize the permissions quite easily regarding the requirement of an organization. This will lead to the improvement of the security level and as well as for better performance.

Keywords:
• How to set SharePoint Permission to a user using PnP PowerShell.
• How to set Unique Permission for list/library using PnP PowerShell.
• How to set Unique Permission to subsite using PnP PowerShell.
• SharePoint permission tasks using PnP PowerShell.

Author: Softree Technology

Leave a Reply