1

I have an event receiver that sets SPField values based on certain user choices. I'm trying to show/hide one field based on the current user's signed in permissions, but only when the list item is being edited.

First of all, what event does this apply to? Is it ItemAdding, ItemUpdating, ItemAdded, etc.? I've tried each of them with no luck.

    public override void ItemUpdating(SPItemEventProperties properties)
    {
        using (SPWeb web = properties.Web)
        {
            SPList list = properties.List;
            SPListItem item = properties.ListItem;
            SPField status = list.Fields["Site Status"];
            SPUser user = web.CurrentUser;
            bool authorized = list.DoesUserHavePermissions(user, SPBasePermissions.ManageWeb);

            if (list != null)
            {
                if (authorized == true)
                {
                    status.Hidden = true;
                    //status.ShowInEditForm = true;  this never works no matter what
                    status.Update();
                }
                else
                {
                    status.Hidden = false;
                    //status.ShowInEditForm = false;  this also never works no matter what
                    status.Update();
                }
            }
        }
    }

So I guess there are 2 questions here. First, what event does this apply to, and second what is wrong with the code?

1
  • 1
    event receivers fire after you click Save button the Edit form.. first ItemUpdating will be fired before the item is actually updated and than ItemUpdated after the item is actually updated.. So moral of the story, you can't use event receivers to hide a field, because it's already too late.. Jun 4, 2014 at 21:28

1 Answer 1

3

Your best bet is to customize the Edit form of the list, and use JavaScript object model to show/hide field based on user permissions..

You can use SPSecurityTrimmedControl as shown here

Or you can use JavaScript/jQuery to hide the field after checking permissions.. See SharePoint 2010: Conditionally Hide Fields on Standard List Forms using jQuery.. It says 2010 but should apply to 2013..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.