0

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

0

It is on the Item itself, not on the folder--my mistake. But the error you are getting suggests that the folder cannot be found, or is in the wrong format. Check to see if the Folder is present. Make sure $SourceFolder is a correct value.

This is not returning anything.

 $Folder = $CommitteeSite.GetFolder( $SourceFolder )  

The $SourceFolder should be in the format of the site relative url. so try this instead.

Add-SPPermissionToFolderGroup $Site $CommitteeSite "/Committees/TRAINING_Training" "SP_TRAINING_Managers" "Contribute"
  • I tried to break inheritance on the folder as you suggested, but got the following error... Method invocation failed because [Microsoft.SharePoint.SPFolder] does not contain a method named 'BreakRoleInheritance'. – Dan Franco Sep 9 '16 at 7:13
  • Check my update. – Mike Sep 20 '16 at 16:13
  • Hi Mike. Many thanks for your input. I tried your suggestion but got the same result. – Dan Franco Sep 22 '16 at 12:14
  • Just realized that your script doesn't include the library. If the library and the site name are the same, it should really be $SpFolder = $CommitteeSite.GetFolder( "/Committees/Committee/Test Folder" ), does $SpFolder have any value? – Mike Sep 23 '16 at 17:54
  • Hi Mike. Many thanks for your input. I 've tried your latest suggestion and get the same result. And yes $SpFolder does get a value, but $SpFolder.Item has no value. – Dan Franco Sep 26 '16 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.