0

I've got a document library and I want to change the permission of each file present in a particular document set (say Office Documents) to Read-Only when an item is Updated in a List (say List A). Can anyone please help me achieve this requirement?

P.s I want to use event receiver to achieve this requirement

0

You need to remove the user's permissions on the file then give the user Read-Only permission on the file.

Here is my demo for you:

 using (SPWeb web = site.OpenWeb())
            {
                SPFolder documentSet = web.GetFolder("http://sp2016/sites/dev/Shared%20Documents/DocumentSet");
                SPPrincipal user = web.EnsureUser("contoso2016\\michael");
                SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
                SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
                roleAssignment.RoleDefinitionBindings.Add(roleDefinitions["Read"]);
                foreach (SPFile file in documentSet.Files)
                {
                    file.CheckOut();
                    SPItem item = file.Item;

                    item.BreakRoleInheritance(true);
                    item.RoleAssignments.Remove(user);
                    item.RoleAssignments.Add(roleAssignment);
                    file.CheckIn("");               
                    item.Update();
                }
| improve this answer | |
0

You can use workflows to do this job or in an eventHandler, remove Delete and edit permissions on this item

item.RoleAssignments.Remove(....)
| improve this answer | |

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.