1

I want to create a Function with a single command line to add a SP Group to a Folder within a Documents Library.

Structure:

  • Site
    • Document Library
      • Folder (Permissions/Group(s) to be added here)
        • Items (Documents)

I have...

function Add-SPPermissionToListGroup
{
param ($Url, $ListName, $GroupName, $PermissionLevel)
$web = Get-SPWeb -Identity $Url
$list = $web.Lists.TryGetList($ListName)
if ($list -ne $null)
{
    if ($list.HasUniqueRoleAssignments -eq $False)
    {
        $list.BreakRoleInheritance($True)
    }
    else
    {
        if ($web.SiteGroups[$GroupName] -ne $null)
        {
            $group = $web.SiteGroups[$GroupName]
            $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
            $roleDefinition = $web.RoleDefinitions[$PermissionLevel];
            $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
            $list.RoleAssignments.Add($roleAssignment)
            $list.Update();
            Write-Host "Successfully added $PermissionLevel permission to $GroupName group in $ListName list. " -foregroundcolor Green
        }
        else
        {
            Write-Host "Group $GroupName does not exist." -foregroundcolor Red
        }
    }
}
$web.Dispose()
}

...which I got from Add group to Library permissions using powershell

Q: How do I modify the above to get what I need? I have tried a few things but no success.

TIA

1 Answer 1

2

All you need to change is the object where you add new role assignment, in you case: - change list to folder. Your modified script:

function Add-SPPermissionToFolderGroup
{
    param ($Url, $sourceFolder, $GroupName, $PermissionLevel)
    $web = Get-SPWeb -Identity $Url

            // $sourceFolderGets the folder that is located at the specified URL.
            $folder = $web.GetFolder($sourceFolder)
            $group = $web.SiteGroups[$GroupName]
            $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
            $roleDefinition = $web.RoleDefinitions[$PermissionLevel];
            $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);

            // 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();
    $web.Dispose()
}
5
  • Thank you, Thank you, Thank you!!! Figured it out. Tried to put the full path in the url and didn't work then tried to add the path after the domain in front of the foldername (sourcefolder) and that worked 100%. :)
    – RaveWolf
    Jul 25, 2014 at 9:35
  • FYI: ur missing the $ by ($true) - $folder.Item.BreakRoleInheritance(true);
    – RaveWolf
    Jul 25, 2014 at 9:40
  • Hi Again. How can I modify the above to Add a single usergroup to every folder within a document library. TIA
    – RaveWolf
    Oct 2, 2014 at 11:34
  • The same way but iterate all folders in library like in this example sharepoint.stackexchange.com/questions/92622/… Oct 2, 2014 at 13:12
  • Yep seems like the only way to do anything with sharepoint powershell is find one-off stuff people have done like this. wonderful job. Dec 23, 2016 at 15:38

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.