0

I created an Event Receiver that prevents an item if a checkbox is ticked however if I add the condition, the event receiver do not fire

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
            {
                SPRemoteEventResult result = new SPRemoteEventResult();
                if (properties.EventType == SPRemoteEventType.ItemDeleting)
                {
                    List list = clientContext.Web.Lists.GetByTitle(properties.ItemEventProperties.ListTitle);
                    ListItem fieldItem = list.GetItemById(properties.ItemEventProperties.ListItemId);

                    if ((bool)fieldItem["CheckBox"])
                    {
                        result.Status = SPRemoteEventServiceStatus.CancelWithError;
                        result.ErrorMessage = "Error";
                    }

                        result.Status = SPRemoteEventServiceStatus.Continue;

                }
                return result;
            }
        }

2 Answers 2

1

Call Load and ExecuteQuery Before accessing item properties.

List list = clientContext.Web.Lists.GetByTitle(properties.ItemEventProperties.ListTitle);
ListItem fieldItem = list.GetItemById(properties.ItemEventProperties.ListItemId);

clientContext.Load(fieldItem);
clientContext.ExecuteQuery();

if((bool)fieldItem["CheckBox"]){
  //....
}
0
0

You can add properties.Cancel = true; to cancel item from deleting.

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
    {
        using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
        {
            SPRemoteEventResult result = new SPRemoteEventResult();
            if (properties.EventType == SPRemoteEventType.ItemDeleting)
            {
                List list = clientContext.Web.Lists.GetByTitle(properties.ItemEventProperties.ListTitle);
                ListItem fieldItem = list.GetItemById(properties.ItemEventProperties.ListItemId);

                if ((bool)fieldItem["CheckBox"])
                {
                    result.Status = SPRemoteEventServiceStatus.CancelWithError;
                    result.ErrorMessage = "Error";
                    properties.Cancel = true;
                }

                    result.Status = SPRemoteEventServiceStatus.Continue;

            }
            return result;
        }
    }

Your Answer

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

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