1

Item level permission from it's native parent. We convert files are uploaded by end user to png put it in "PNG" folder of that document library. There are bunch of other folders (_Private, PNG, Public, Finance, HR) within a document library (including png folder which has read access to all the custom groups).

When HR uploads a file (i.e. payroll.xls) into their folder "HR" then it gets rendered to PNG format (payroll.png). Rendered file resides in the PNG folder. I like to inherit permission from HR folder and apply it on the rendered png file so that only HR can see payroll.png when HR go to "PNG" Folder. The file should not be visible by unintended group(s).

Hope it's making sense.

0

3 Answers 3

2

Here is the code that copies security from source item to target item where target item resides in another folder within the same document library

private void InheritPermission(Guid siteID, Guid webID, Guid ListID, int SourceItemID,  int ImageItemID )
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteID))
    {
        using (SPWeb web = site.OpenWeb(webID))
        {
            SPList list = web.Lists[ListID];
            SPListItem SourceItem = list.GetItemById(SourceItemID);
            SPListItem ImageItem = list.GetItemById(ImageItemID);

if (!ImageItem.HasUniqueRoleAssignments) { ImageItem.BreakRoleInheritance(true); } SPRoleAssignmentCollection ImageItemRoles = ImageItem.RoleAssignments; //Removing all the default groups while (ImageItemRoles.Count > 0) ImageItemRoles.Remove(0); SPRoleAssignmentCollection SourceItemRoles = SourceItem.RoleAssignments; foreach (SPRoleAssignment SourceRole in SourceItemRoles) { ImageItem.RoleAssignments.Add(SourceRole); } ImageItem.Update(); } }

}); }

1

You would have to break role inheritance on the item placed in the PNG folder. You would then have to manually add the same security to the item that the HR folder items have.

The easiest way to do this is add the PNG to the HR folder first, then call SPListItem.BreakRoleInheritance(true). This will copy the permissions to the item. Then I would move the item to the PNG folder.

0
  1. You would need to have an event receiver on your library coded to operate when an item of the type PNG is added under the PNG folder.(OR you can add the logic below to the same code which creates this item under PNG folder.)
  2. Further on the ItemAdded event get the RoleAssignment of any file in the HR folder.
  3. On the newly created item under PNG folder, break the RoleInheritance and then add role assignment from #3 to the same.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.