sharepoint 2010/moss2007 for the document library to upload files and new folder, files and folders to modify the default permissions (do not inherit the permissions of the document library)

A project the following requirements: users upload to document library file or folder in the new document library, they can set permissions, and the newly uploaded files, or the new folder for their ownit has "full Control permissions to other users only have read access, the user has the right to allow other users to have participate in the discussion," or "permission settings" permission. As we all know, a newly uploaded file, or a new document library, their authority is to inherit the permissions of the document library, document library privileges for a user for at least participate in the discussion, because in has a "participation the discussion of "permission, users can upload a file and new folder action, so they need an event to control the new upload files and the default permissions new Folder. Write two "EventHandler".

One EventHandler is for the New Create Folder, this EventHandler is same to the list EventHandler, without further ado, the code is as follows:

public class NewDocumentControlPermission : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            //base.ItemAdded(properties);
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(properties.SiteId))
                    {
                        using (SPWeb web = site.OpenWeb(properties.OpenWeb().ID))
                        {
  
                            web.AllowUnsafeUpdates = true;
  
                            SPUser user = web.Users.GetByID(properties.CurrentUserId);
                            SPListItem item = properties.ListItem;
                            //Get the login user account who call the event
                            SPRoleAssignment ra1 = new SPRoleAssignment(web.EnsureUser(user.LoginName));
                            ra1.RoleDefinitionBindings.Add(web.RoleDefinitions["Full Control"]);
                            item.BreakRoleInheritance(true);
                            item.RoleAssignments.Add(ra1);
  
  
                            for (int count = 0; count < item.RoleAssignments.Count; count++)
                            {
                                SPRoleAssignment spra = item.RoleAssignments[count];
                                for (int i = 0; i < spra.RoleDefinitionBindings.Count; i++)
                                {
                                    //if (spra.RoleDefinitionBindings[i].Name == "Limited Access")
                                    //{
                                    //    item.BreakRoleInheritance(true);
                                    //    item.RoleAssignments.Remove(spra.Member);
                                    //}
                                    if (spra.RoleDefinitionBindings[i].Name == "Participate in the discussion")
                                    {
                                        if (!spra.RoleDefinitionBindings.Contains(web.RoleDefinitions["Read"]))
                                        {
                                            spra.RoleDefinitionBindings.Add(web.RoleDefinitions["Read"]);
                                        }
                                        spra.RoleDefinitionBindings.Remove(web.RoleDefinitions["Participate in the discussion"]);
                                        //item.RoleAssignments[count].RoleDefinitionBindings[i].Update();
                                        spra.Update();
                                        item.SystemUpdate(false);
                                    }
                                    else if (spra.RoleDefinitionBindings[i].Name == "User Permission")
                                    {
                                        if (!spra.RoleDefinitionBindings.Contains(web.RoleDefinitions["Read"]))
                                        {
                                            spra.RoleDefinitionBindings.Add(web.RoleDefinitions["Read"]);
                                        }
                                        spra.RoleDefinitionBindings.Remove(web.RoleDefinitions["User Permission"]);
                                        //spra.RoleDefinitionBindings[i].Update();
                                        spra.Update();
                                        item.SystemUpdate(false);
                                    }
                                }
                            }
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                });
            }
            catch { }
        }
    }
 

Specifically how to deploy this EventHandler, not repeat them here.

Another EventHandler is for the upload a new file, thie EventHandler is different from the list EventHandler, without further ado, the following code:

public class ControlPermission : IListEventSink
    {
        void IListEventSink.OnEvent(Microsoft.SharePoint.SPListEvent listEvent)
        {
            try
            {
                if (listEvent.Type == SPListEventType.Insert)//Add{}
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPWeb web = listEvent.Site.OpenWeb();
                        SPFile file = web.GetFile(listEvent.UrlAfter);
                        SPListItem item = file.Item;
  
                        //Add a personal full Control
                        SPUser user = file.Author;
                        SPRoleAssignment sa = new SPRoleAssignment((SPPrincipal)user);
                        SPRoleDefinition role = web.RoleDefinitions["Full Control"];
                        sa.RoleDefinitionBindings.Add(role);
                        item.BreakRoleInheritance(true);
                        item.RoleAssignments.Add(sa);
  
                        //Modify the entire department for read access
                        for (int count = 0; count < item.RoleAssignments.Count; count++)
                        {
                            SPRoleAssignment spra = item.RoleAssignments[count];
                            for (int i = 0; i < spra.RoleDefinitionBindings.Count; i++)
                            {
                                //if (spra.RoleDefinitionBindings[i].Name == "Limited Access")
                                //{
                                //    item.BreakRoleInheritance(true);
                                //    item.RoleAssignments.Remove(spra.Member);
                                //}
                                if (spra.RoleDefinitionBindings[i].Name == "Participate in the discussion")
                                {
                                    if (!spra.RoleDefinitionBindings.Contains(web.RoleDefinitions["Read"]))
                                    {
                                        spra.RoleDefinitionBindings.Add(web.RoleDefinitions["Read"]);
                                    }
                                    spra.RoleDefinitionBindings.Remove(web.RoleDefinitions["Participate in the discussion"]);
                                    //item.RoleAssignments[count].RoleDefinitionBindings[i].Update();
                                    spra.Update();
                                    item.SystemUpdate(false);
                                }
                                else if (spra.RoleDefinitionBindings[i].Name == "User Permission")
                                {
                                    if (!spra.RoleDefinitionBindings.Contains(web.RoleDefinitions["Read"]))
                                    {
                                        spra.RoleDefinitionBindings.Add(web.RoleDefinitions["Read"]);
                                    }
                                    spra.RoleDefinitionBindings.Remove(web.RoleDefinitions["User Permission"]);
                                    //spra.RoleDefinitionBindings[i].Update();
                                    spra.Update();
                                    item.SystemUpdate(false);
                                }
                            }
                        }
                    });
                }
            }
            catch { }
        }
    }
 
Note: here the "User Permission" is the author's own add a permission in SharePoint 2010/Moss2007, to check this permission, users can manage their own document library files and folder permissions.

Related Posts