Hi all,

I've got a fairly straightforward event handler on a document library that uses 
ItemAdded to apply permissions on a document based on a column value in the CT. 
This is working quite well with only a few limitations. The main one being that 
items being updated (either via update copy or overwrite) are not being 
affected.

Excuse my naivety but could I simply re-use the code below, replacing ItemAdded 
with ItemUpdated, and push out a new event type to attach to the library?

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace CEO.Intranet.DocumentCentre.Library
{
    public class ItemEventHandler: SPItemEventReceiver
    {

        public override void ItemAdded(SPItemEventProperties properties)
        {

                base.ItemAdded(properties);
                SPListItem originalItem = properties.ListItem;
                string url = properties.WebUrl;
                SPListItem item = null;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    try
                    {
                        using (SPSite site = new SPSite(url))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                item = 
web.Lists[properties.ListId].GetItemById(properties.ListItemId);

                            }
                        }
                    }
                    catch (Exception)
                    {
                        try
                        {
                            using (SPSite site = new SPSite(url))
                            {
                                using (SPWeb web = site.OpenWeb())
                                {
                                    SPList list = 
web.Lists[originalItem.ParentList.ID];
                                    item = list.Items[originalItem.UniqueId];

                                }
                            }
                        }
                        catch (Exception ex1)
                        {
                            Console.WriteLine(ex1.Message);
                        }
                    }

                    if (item != null)
                        SetSecurityForNewItem(item);


                });

        }

        private void SetSecurityForNewItem(SPListItem newItem)
        {
            //SPListItem newItem = newFile.Item;
            try
            {

                newItem.BreakRoleInheritance(true);

                SPGroupCollection groupCollection = 
newItem.ParentList.ParentWeb.Groups;
                SPRoleDefinitionCollection roleDefinitions = 
newItem.ParentList.ParentWeb.RoleDefinitions;
                char[] seperator = new char[] { ';' };
                string groupPermissions = newItem["Group 
Permissions"].ToString();
                string[] permissionGroups = groupPermissions.Split(seperator);
                bool removeReadPermissions = true;
                if (permissionGroups[1] != "#All Portal Users")
                {
                    foreach (SPGroup spGroup in groupCollection)
                    {
                        removeReadPermissions = true;
                        foreach (string subString in permissionGroups)
                        {

                            if (subString.Length > 1 && spGroup.Name == 
subString.Substring(1))
                            {
                                removeReadPermissions = false;
                            }
                        }
                        if (removeReadPermissions)
                        {
                            SPRoleAssignmentCollection roleAssignmentCollection 
= newItem.RoleAssignments;
                            foreach (SPRoleAssignment roleAssignment in 
roleAssignmentCollection)
                            {
                                if(roleAssignment.Member.Name == spGroup.Name)
                                {
                                    SPRoleDefinitionBindingCollection 
roleDefBindings = roleAssignment.RoleDefinitionBindings;
                                    if 
(roleDefBindings.Contains(roleDefinitions["Read"]))
                                    {
                                        
roleDefBindings.Remove(roleDefinitions["Read"]);
                                        roleAssignment.Update();
                                    }
                                }
                            }

                        }
                     }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Regards,

Paul
Online Developer, ICT
CEO Sydney

--------------------------------------------------------------------------------
Support procedure: http://www.codify.com/lists/support
List address: [email protected]
Subscribe: [email protected]
Unsubscribe: [email protected]
List FAQ: http://www.codify.com/lists/ozmoss
Other lists you might want to join: http://www.codify.com/lists

Reply via email to