Change the permission of all the files in a Document Set of a Document Library to Read-Only
-
08-02-2021 - |
Pergunta
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
Solução
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();
}
Outras dicas
You can use workflows to do this job or in an eventHandler, remove Delete and edit permissions on this item
item.RoleAssignments.Remove(....)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange