Should be ok.

Be sure to run it through SPDisposeChecker 
(http://code.msdn.microsoft.com/SPDisposeCheck).  Event receivers have the 
potential to run a lot and should be checked every time, because if they leak 
you will find out very quickly :-)

See http://msdn.microsoft.com/en-us/library/aa973248.aspx


Regards,

Paul Turner
Senior Solutions Specialist

M: 0412 748 168 P: 08 8238 0912 F: 08 8234 5966
A: 66 Henley Beach Road, Mile End SA 5031
E: [email protected]<mailto:[email protected]>  W: 
www.dws.com.au<https://webmail.dws.com.au/owa/UrlBlockedError.aspx>
     <http://www.msteched.com/australia/Public/default.aspx>
ADVANCED BUSINESS SOLUTIONS LTD

This email and any files transmitted with it are confidential and are only for 
the use of the person to whom they are addressed. If you are not the intended 
recipient you have received this email in error and are requested to delete it 
immediately. Any opinion expressed in this e-mail may not necessarily be that 
of DWS Pty Ltd.
Please consider the environment before printing this email.
________________________________
From: [email protected] [[email protected]] On Behalf Of Paul Noone 
[[email protected]]
Sent: Monday, 3 August 2009 12:08 PM
To: [email protected]
Subject: Event handler - ItemUpdated

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: https://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
--------------------------------------------------------------------------------
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