Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Event receiver not working as hoped

$
0
0

Hi,

I want to trap items in a list being deleted and stop it.
I can do this easily and all works well... but now I want to check to see if the current user is a member of an owners group.
If they are then allow delete, if not then trap and cancel the event.

My account is a member of the owner group and can delete no problem.
However I have logged on as a user who has contribute but is not in the owners group and can still delete.

Is there a problem with my logic or have I missed something?

public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);

            using (SPSite site = new SPSite(properties.WebUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    int _currentUser = properties.CurrentUserId;
                    SPUser spCurrentUser = web.AllUsers[_currentUser];
                    SPGroup ownerGroup = web.Groups["Sandpit Owners"];
                    if (spCurrentUser.Groups[ownerGroup.ID] != null)
                    {
                        properties.Cancel = true;
                        properties.ErrorMessage = "Deleting announcements is not allowed!";
                    }
                    else
                    {
                        properties.Cancel = false;
                        properties.Status = SPEventReceiverStatus.Continue;
                    }
                }
            }


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>