2

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); } } } }

}

2
  • I'm doing it right? With the code on top?
    – alien_ru
    Commented Dec 18, 2012 at 8:56
  • post another question! Commented Dec 20, 2012 at 9:55

1 Answer 1

0

You can bind the existing groups in your SPUser by something like:

SPGroupCollection webGroups = currentUser.Groups;
checkboxlist.DataSource = webGroups;
checkboxlist.DataValueField = "ID";
checkboxlist.DataTextField = "Name";
checkboxlist.DataBind();

Then for giving permissions to particular document/list item.. Iterate through the selected groups by user and do something like:

//Break the role inheritance from List and remove any RoleAssignments
item.BreakRoleInheritance(false);
while (item.RoleAssignments.Count > 0)  
{ 
    item.RoleAssignments.Remove(0); 
}

foreach (ListItem li in checkboxlist.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 = workspace.RoleAssignments.GetAssignmentByPrincipal(principalGroup);

        item.RoleAssignments.Add(roleAssignment);
    }
}
11
  • If replaced SPGroupCollection webGroups = web.Groups; by SPGroupCollection webGroups = currentUser.Groups; ? For current user.
    – alien_ru
    Commented Dec 17, 2012 at 10:58
  • yes exactly.... Commented Dec 17, 2012 at 11:12
  • How to create a web part to display all of this? I simply just am teaching c # and shrepoint foundation in 2010 and I still do not know how to create web forms and add own code there. Tell me please: how to do it.
    – alien_ru
    Commented Dec 17, 2012 at 13:35
  • Open Visual Studio 2010 Ultimate -> New Project -> SharePoint tab -> select Visual Web Part! You will have a user control, put your code logic in Visual Web Part and deploy the solution! You can insert this Visual Web Part on any page containing Web Part Zone to use it... Commented Dec 17, 2012 at 13:37
  • Do you have examples? For example, I would be easier to do it.
    – alien_ru
    Commented Dec 17, 2012 at 13:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.