How me in web form to list the groups the current user checkboxes to select one or more groups? What would then set the permission to a document on the basis of chosen groups?
I'm doing it right:
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomGroupAssignment.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Web.Url);
SPWeb web = site.OpenWeb();
SPUser currentUser = web.CurrentUser;
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
// CheckBoxList1 = new CheckBoxList();
SPGroupCollection webGroups = currentUser.Groups;
CheckBoxList1.DataSource = webGroups;
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataBind();
//Break the role inheritance from List and remove any RoleAssignments
docLib.BreakRoleInheritance(false);
while (docLib.RoleAssignments.Count > 0)
{
docLib.RoleAssignments.Remove(0);
}
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
// Give permissions to a specific group
SPGroup group = web.Groups.GetByID(Convert.ToInt32(li.Value));
SPPrincipal principalGroup = group;
SPRoleAssignment roleassignment_group = new SPRoleAssignment(group);
SPRoleAssignment roleAssignment = web.RoleAssignments.GetAssignmentByPrincipal(principalGroup);
docLib.RoleAssignments.Add(roleAssignment);
}
}
}
}
}