Forum Discussion
vaibhavk1925
Jun 16, 2022Copper Contributor
Remove permissions from library and add users sharepoint online
Hello, below powershell script is to create multiple libraries and then remove existing permissions and then adding a new user permissions. Its creating document libraries in sharepoint online but fails to remove existing permissions and add new user permissions. Please help
#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"
$UserName=""
$Password =""
$AdminURL = ""
$UserID = "email address removed for privacy reasons"
Connect-PnPOnline -Url $AdminURL -Credential $Cred
#Function to Create a SharePoint Online document library
Function CreateSPO-DocumentLibrary()
{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$True,ValueFromPipeline)] [String] $LibraryName,
[Parameter(Mandatory=$False)] [String] $Description,
[Parameter(Mandatory=$False)] [Switch] $ShowOnQuickLaunch
)
Try {
Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
#Get All Existing Lists from the web
$Lists = $Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
#Check if Library name doesn't exist already
If(!($Lists.Title -contains $LibraryName))
{
#Set Quick Launch Option
If($ShowOnQuickLaunch.IsPresent)
{
$QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::On
}
Else
{
$QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::Off
}
#Create Document Library
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $LibraryName
$ListInfo.Description = $Description
$ListInfo.TemplateType = [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary
$List = $Web.Lists.Add($ListInfo)
$List.OnQuickLaunch = $QuickLaunchOption
#Set-PnPList -Identity $LibraryName -BreakRoleInheritance
$List.BreakRoleInheritance($true, $false)
Set-PnPListPermission -Identity $LibraryName -AddRole "Edit" -User $UserID
#Set-PnPList -Identity $ListInfo.Title -BreakRoleInheritance -ClearSubscopes
# Set-PnPList -Identity $LibraryName -EnableVersioning $True -EnableMinorVersions $True -CopyRoleAssignments:$False -BreakRoleInheritance:$True -ListExperience NewExperience
# load the list role assignments
#$Ctx.Load($listName.RoleAssignments)
#$Ctx.ExecuteQuery()
# remove the visitor group from the list
#$listName.RoleAssignments.Groups.Remove($Group)
#$Ctx.ExecuteQuery()
$RoleAssignmentCount = $List.RoleAssignments.Count
#Remove All Permissions from the List
For ($i= $RoleAssignmentCount-1; $i -ge 0; $i--)
{
Write-host "Removing Permission from:"$List.RoleAssignments[$i].Member.name
$List.RoleAssignments.Remove($i)
}
$List.Update()
$Ctx.ExecuteQuery()
write-host -f Green "`tNew Document Library '$LibraryName' has been created!"
}
Else
{
Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exists!"
}
}
Catch {
write-host -f Red "`tError:" $_.Exception.Message
}
}
#Set Parameters
$CSVFilePath = "C:\Folder\DocumentLibs.csv"
#Get Credentials to connect
#$Cred = Get-Credential
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $(convertto-securestring $Password -asplaintext -force)
Connect-SPOService -Url $AdminURL -Credential $Cred
#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath
#Read CSV file and create a document library
ForEach($Line in $CSVFile)
{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Line.SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the web from URL
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.executeQuery()
#Get the ShowOnQuickLaunch option
$ShowOnQuickLaunch = [System.Convert]::ToBoolean($Line.ShowOnQuickLaunch)
#Call the function to create document library
If($ShowOnQuickLaunch -eq $True)
{
CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description -ShowOnQuickLaunch
}
Else
{
CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description
}
}
No RepliesBe the first to reply