Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

}

share|improve this question
    
I'm doing it right? With the code on top? –  alien_ru Dec 18 '12 at 8:56
    
post another question! –  Arsalan Adam Khatri Dec 20 '12 at 9:55
add comment

1 Answer

up vote 0 down vote accepted

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);
    }
}
share|improve this answer
    
If replaced SPGroupCollection webGroups = web.Groups; by SPGroupCollection webGroups = currentUser.Groups; ? For current user. –  alien_ru Dec 17 '12 at 10:58
    
yes exactly.... –  Arsalan Adam Khatri Dec 17 '12 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 Dec 17 '12 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... –  Arsalan Adam Khatri Dec 17 '12 at 13:37
    
Do you have examples? For example, I would be easier to do it. –  alien_ru Dec 17 '12 at 13:44
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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