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 can I get a list of groups in a form or dialog box to select one or more groups, and then assign permissions to the document? Here in this code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace ItemAdded.EventReceiver1
{
    /// <summary>
    /// События элемента списка
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// Добавлен элемент.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
           SPSite site = new SPSite(properties.WebUrl);

           SPWeb web = site.OpenWeb();
           SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
           SPGroup oGroup = web.SiteGroups["Програмисты"];
           var groups2 = new SPFieldUserValueCollection();
           groups2.Add(new SPFieldUserValue(web, oGroup.ID, oGroup.Name));
           SPUser currentUser = web.CurrentUser;
           SPGroupCollection asd = currentUser.Groups;
           var groups = new SPFieldUserValueCollection();
           foreach (SPGroup group in asd)
           {

               //foreach (SPFieldUserValue group in groups)
               groups.Add(new SPFieldUserValue(web, group.ID, group.Name));

           }
           EventFiringEnabled = false;
           // properties.ListItem["ответственный"] = oGroup;
           properties.ListItem["ответственный"] = groups + ";#" + groups2;
           properties.ListItem.Update();
           EventFiringEnabled = true;

           var userValues = (SPFieldUserValueCollection)properties.ListItem["ответственный"];
           foreach (SPFieldUserValue userValue in userValues)
           {
               var user = properties.Web.SiteGroups.GetByID(userValue.LookupId);
               SetSPListItemUserPermissions(properties.ListItem, user, SPRoleType.Contributor);
           }

       }

       public void SetSPListItemUserPermissions(SPListItem listItem, SPPrincipal Principal, SPRoleType roleType)
       {
           //Break role inheritance
           if (!listItem.HasUniqueRoleAssignments)
               listItem.ResetRoleInheritance();
           EventFiringEnabled = false;
           listItem.Update();
           listItem.BreakRoleInheritance(false);
           listItem.Update();

           //Create new role
           var roleDefinition = listItem.Web.RoleDefinitions.GetByType(roleType);
           var roleAssignment = new SPRoleAssignment(Principal);
           roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

           //Assign new role
           listItem.RoleAssignments.Add(roleAssignment);
           listItem.Update();
           EventFiringEnabled = true;
       }
    }
}
share|improve this question
1  
Before coming to Event Receiver you should know what to do, since during Event Receiver's you cannot take anything as input from the user! Please tell us your scenario, we will tell the alternative! –  Arsalan Adam Khatri Dec 12 '12 at 15:38
    
The scenario is this: user create a document in a library. After you create or add it to the library in the document are assigned according to the rights according to groups he is a member. Before the assigned rights to a document, need to highlight a list of these groups with the ability to select one or more. Sorry for the bad English. –  alien_ru Dec 13 '12 at 7:12
    
Maybe I need to use CheckboxList? But how can i transmit the found groups to CheckboxList? –  alien_ru Dec 13 '12 at 14:13
    
You need to take the groups input before adding the document... Yes you can use checkbox list or two ListBoxes with selecting from one and adding to another... but then don't use Event Receiver... make a Visual Web Part to do that..! –  Arsalan Adam Khatri Dec 13 '12 at 23:24
    
Do you have an example of such a solution? I'm just a novice programmer sharepoint. –  alien_ru Dec 14 '12 at 7:02
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.