locked
Problem to add Permissions To Users RRS feed

  • Question

  • Hello,

    I want to manage permissions for each item of List. In my List I have to fields "Readers" and "Contributors". When user want to add a new item, he must write also the readers and the contributors in the appropiate fields. So, I developped an eventHandler "ItemAdded" that modifiy permessions, but when I try to excute it I don't have a good result. this is my code
     public override void ItemAdded(SPItemEventProperties properties)
            {
                this.DisableEventFiring();
                base.ItemAdded(properties);
                SPWeb web = null;
                try
                {
    
                    web = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl);
    
                    SPListItem item = web.Lists[properties.ListId].GetItemById(properties.ListItemId);
    
                  
    
                    item.BreakRoleInheritance(false);
    
                    
    
                    #region Add full Control to Author
                    String loginAdmin = properties.UserLoginName;
                    SPUser userAdmin = web.Users[loginAdmin];
    
                    SPRoleDefinition roleDefAdmin = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
    
                    SPRoleAssignment roleAssignementAdmin = new SPRoleAssignment(userAdmin);
    
                    roleAssignementAdmin.RoleDefinitionBindings.Add(roleDefAdmin);
    
                    item.RoleAssignments.Add(roleAssignementAdmin);
                    
                    #endregion
    
    
                    #region Add Reader
                    SPFieldUserValueCollection usersvaluesLecteurs = (SPFieldUserValueCollection)item["Lecteurs"];
    
                    for (int i = 0; i < usersvaluesLecteurs.Count; i++)
                    {
                        SPUser userLecteur = usersvaluesLecteurs[i].User;
    
                      
    
                        SPRoleDefinition roleDefLecteur = web.RoleDefinitions.GetByType(SPRoleType.Reader);
    
                        SPRoleAssignment roleAssignementLecteur = new SPRoleAssignment(userLecteur);
    
                        roleAssignementLecteur.RoleDefinitionBindings.Add(roleDefLecteur);
    
                        item.RoleAssignments.Add(roleAssignementLecteur);
    
    
                    }
                    #endregion
    
                    #region Add Contributor
                    SPFieldUserValueCollection usersvaluesModificateurs = (SPFieldUserValueCollection)item["Modificateurs"];
    
                    for (int i = 0; i < usersvaluesModificateurs.Count; i++)
                    {
                        SPUser userModificateur = usersvaluesModificateurs[i].User;
    
                        //on ajoute le user sur le dossier
    
                        SPRoleDefinition roleDefModificateur = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
    
                        SPRoleAssignment roleAssignementModificateur = new SPRoleAssignment(userModificateur);
    
                        roleAssignementModificateur.RoleDefinitionBindings.Add(roleDefModificateur);
    
                        item.RoleAssignments.Add(roleAssignementModificateur);
    
                    }
                    #endregion
    
                    item.Update();
                    this.EnableEventFiring();
    
                }
    
                catch (Exception ex)
                {
    
                    properties.Cancel = true;
    
                    properties.ErrorMessage = "Erreur dans l'Event ItemAdded : " + ex.Message;
    
                }
    
                finally
                {
    
                    if (web != null)
    
                        web.Dispose();
                }
    This my problem : I added an item created by Administrator and I added a user1 for reading and modifying. After, I change the current user to user1, but I didn't see anything in my List (I must see the Item because user1 is a reader).

    I hope that you can help me.
    Thank you
    Sunday, April 19, 2009 12:09 PM