From f55347baf2b62d5bc2a3f5e0a0015b363d1fb31f Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sun, 22 May 2022 00:25:58 +0300 Subject: [PATCH] Fix #1882 - issue with Set-PnPListPermission --- CHANGELOG.md | 3 +- src/Commands/Lists/SetListPermission.cs | 88 +++++++++++++------------ 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08b2e1a69..97e4c981a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +33,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Fixed `Get-PnPGroup` , `Get-PnPGroupPermissions` and `Set-PnPGroupPermissions ` cmdlets by making them more consistent. They will also throw error if a group is not found. [#1808](https://github.com/pnp/powershell/pull/1808) - Fixed `Get-PnPFile` issue with every 3rd file download in PS 5. - Fixed `Add-PnPContentTypesFromContentTypeHub`, if `Site` parameter is specified, it will be used now to sync content types from content type hub site. -- Fixed `Get-PnPTeamsTeam`, the cmdlet now also returns additional properties like `WebUrl, CreatedDateTime, InternalId` [#1825](https://github.com/pnp/powershell/pull/1825) +- Fixed `Get-PnPTeamsTeam`, the cmdlet now also returns additional properties like `WebUrl, CreatedDateTime, InternalId`. [#1825](https://github.com/pnp/powershell/pull/1825) +- Fixed `Set-PnPListPermission`, it will now throw error if the list does not exist. ### Removed - Removed `Get-PnPAvailableClientSideComponents`. Use `Get-PnPPageComponent -Page -ListAvailable` instead. [#1833](https://github.com/pnp/powershell/pull/1833) diff --git a/src/Commands/Lists/SetListPermission.cs b/src/Commands/Lists/SetListPermission.cs index e58613fe0..174ad8394 100644 --- a/src/Commands/Lists/SetListPermission.cs +++ b/src/Commands/Lists/SetListPermission.cs @@ -31,60 +31,62 @@ protected override void ExecuteCmdlet() { var list = Identity.GetList(CurrentWeb); - if (list != null) + if (list == null) { - Principal principal = null; - if (ParameterSetName == "Group") + throw new PSArgumentException($"No list found with id, title or url '{Identity}'", "Identity"); + } + + Principal principal = null; + if (ParameterSetName == "Group") + { + if (Group.Id != -1) { - if (Group.Id != -1) - { - principal = CurrentWeb.SiteGroups.GetById(Group.Id); - } - else if (!string.IsNullOrEmpty(Group.Name)) - { - principal = CurrentWeb.SiteGroups.GetByName(Group.Name); - } - else if (Group.Group != null) - { - principal = Group.Group; - } + principal = CurrentWeb.SiteGroups.GetById(Group.Id); + } + else if (!string.IsNullOrEmpty(Group.Name)) + { + principal = CurrentWeb.SiteGroups.GetByName(Group.Name); } - else + else if (Group.Group != null) { - principal = CurrentWeb.EnsureUser(User); + principal = Group.Group; + } + } + else + { + principal = CurrentWeb.EnsureUser(User); + ClientContext.ExecuteQueryRetry(); + } + if (principal != null) + { + if (!string.IsNullOrEmpty(AddRole)) + { + var roleDefinition = CurrentWeb.RoleDefinitions.GetByName(AddRole); + var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext); + roleDefinitionBindings.Add(roleDefinition); + var roleAssignments = list.RoleAssignments; + roleAssignments.Add(principal, roleDefinitionBindings); + ClientContext.Load(roleAssignments); ClientContext.ExecuteQueryRetry(); } - if (principal != null) + if (!string.IsNullOrEmpty(RemoveRole)) { - if (!string.IsNullOrEmpty(AddRole)) - { - var roleDefinition = CurrentWeb.RoleDefinitions.GetByName(AddRole); - var roleDefinitionBindings = new RoleDefinitionBindingCollection(ClientContext); - roleDefinitionBindings.Add(roleDefinition); - var roleAssignments = list.RoleAssignments; - roleAssignments.Add(principal, roleDefinitionBindings); - ClientContext.Load(roleAssignments); - ClientContext.ExecuteQueryRetry(); - } - if (!string.IsNullOrEmpty(RemoveRole)) + var roleAssignment = list.RoleAssignments.GetByPrincipal(principal); + var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings; + ClientContext.Load(roleDefinitionBindings); + ClientContext.ExecuteQueryRetry(); + foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole)) { - var roleAssignment = list.RoleAssignments.GetByPrincipal(principal); - var roleDefinitionBindings = roleAssignment.RoleDefinitionBindings; - ClientContext.Load(roleDefinitionBindings); + roleDefinitionBindings.Remove(roleDefinition); + roleAssignment.Update(); ClientContext.ExecuteQueryRetry(); - foreach (var roleDefinition in roleDefinitionBindings.Where(roleDefinition => roleDefinition.Name == RemoveRole)) - { - roleDefinitionBindings.Remove(roleDefinition); - roleAssignment.Update(); - ClientContext.ExecuteQueryRetry(); - break; - } + break; } } - else - { - WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null)); - } + } + else + { + WriteError(new ErrorRecord(new Exception("Principal not found"), "1", ErrorCategory.ObjectNotFound, null)); } } }