How to give different permission programmatically on Document Library for different group users?
-
30-09-2020 - |
Domanda
I have document library & 3 groups(AdminGroup,Reviewer,Distributor). I have already give a permission on groups, AdminGroup has Full Control, Reviewer has Reader & Distributor has None :
roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Administrator); // Admin
roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader); // Reviewer
roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.None); // Distributor
Now, I want to give permission on document library that :
- If anyone upload document it's Draft version should only visible to Reviewer,Admin & owner of the document. Distributor user can't see it if he is not owner.
- Reviewer Group's user can only view/edit the document & publish it but can't Approve/Reject it.
- Admin has full control. He can Add/Edit/View & Approve/reject the document.
- Distributor Group's user can only view his own documents & Approved documents.
Soluzione 2
I have found the solution to assign permission to group:
using (SPSite spSite = SPContext.Current.Site)
{
using (SPWeb web = spSite.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPGroupCollection grpColl = SPContext.Current.Site.OpenWeb().SiteGroups;
SPGroup spGroup = grpColl.GetByName("myGroup");
SPRoleAssignment roleAssignment = new SPRoleAssignment(spGroup);
SPRoleDefinition roleDefinition = new SPRoleDefinition();
roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Administrator); // for admin role
SPRoleDefinitionBindingCollection roleDefinitionBindingCollection = new SPRoleDefinitionBindingCollection();
roleDefinitionBindingCollection.Add(roleDefinition);
roleAssignment.ImportRoleDefinitionBindings(roleDefinitionBindingCollection);
web.AllowUnsafeUpdates = true;
web.RoleAssignments.Add(roleAssignment);
web.update();
web.AllowUnsafeUpdates = false;
}}
Then, Give same permission to Document Library.
Altri suggerimenti
Attach a event recevier and add item added / updated event to it for assigneing permission.
First break permission on item and then add required permission as per item status / metadata.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange