In trying to discover how to grant a group permission on a document library folder I came upon the following StackExchange article:
Add Group to Folder in a Document Library using Powershell
I have implemented a "test" version for my purposes before applying it to my code.
function Add-SPPermissionToFolderGroup
{
param ( $Site, $CommitteeSite, $SourceFolder, $GroupName, $PermissionLevel )
#$sourceFolderGets the folder that is located at the specified URL.
$Folder = $CommitteeSite.GetFolder( $SourceFolder )
$Group = $Site.SiteGroups[$GroupName]
$RoleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($Group)
$RoleDefinition = $Site.RoleDefinitions[$PermissionLevel];
https://sharepoint.stackexchange.com/questions/109840/add-group-to-folder-in-a-document-library-using-powershell
#break role inheritance for folders/files because they will be having permissions separate from their parent file/folder
$Folder.Item.BreakRoleInheritance($true);
#apply the new roleassignment to the folder. You can do this at the listitem level if desired (i.e. this could be SPfile.Item.... instead of SPFolder.Item)
$Folder.Item.RoleAssignments.Add($RoleAssignment);
$Folder.Item.Update();
}
$Site = Get-SPWeb "https://SiteCollectionURL/"
$CommitteeSite = Get-SPWeb "https://SiteCollectionURL/Committees"
$CommitteeLib = "Committees"
$Library = $CommitteeSite.Lists[$CommitteeLib]
Add-SPPermissionToFolderGroup $Site $CommitteeSite "TRAINING_Training" "SP_TRAINING_Managers" "Contribute"
When I execute this I get an issue at line 12:
You cannot call a method on a null-valued expression. At line:12 char:5 + $Folder.Item.BreakRoleInheritance($true); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
I get similar issue when trying to run the code below to change the Folders' content type to "Committee"
$CommitteeLib = "Committees"
$Library = $CommitteeSite.Lists[$CommitteeLib]
$LibraryCT = $Library.ContentTypes["Committee"]
$SpFolder = $CommitteeSite.GetFolder( "Committees/Test Folder" )
$Item = $SpFolder.Item
$Item["Content Type ID"] = $LibraryCT.ID
$Itemp.Update()
The issue occurs in the second last line, and then the error is a null valued $Item.
Any ideas on where I'm going wrong.
Many thanks.
Dan