0

Is it possible to extract information how many times did event-receiver fired on list per "action"*.

Let say I have list with ten list items on it. User wants to delete three of them. What I'd like to know is something like this possible in the code behind (on item receiver).

    public override void ItemDeleting (SPItemEventProperties properties)
    {

      if (properties.EventOnListAlreadyFired == true)
        {
        //do something
        }
      else //properties.EventOnListAlreadyFired == false
        {
        //do something else
        }
    }

What I want to do is to update Error Message with custom warning and if some conditions are not met to do

properties.CancelWithError

for the first item in the list. For every other marked item I want to simply

properties.CancelNoError

I'm coding against Sharepoint 2013.

P.S. * by "action" I mean when user clicks "Delete Item(s)" or "Stop changing item(s)".

1

You can use the Property Bag of the List to store a flag after the event has fired.

The property bag is a collection of key/value pairs used to store additional data at different levels of a SharePoint Farm.

So your code will look something like this (not tested):

public override void ItemDeleting (SPItemEventProperties properties)
{   
    var rootFolder = properties.List.RootFolder;
    var eventFiredKey = "EventOnListAlreadyFired";

    //If value is not found, it will return null.
    var alreadyFiredProperty = rootFolder.Properties[eventFiredKey];

    //Event was fired already
    if (alreadyFiredProperty != null)
    {
        //do something
    }
    else 
    {
        //Event was not fired already. Execute logic and Update the Property Bag of the list.
        rootFolder.Properties[eventFiredKey] = true;
        rootFolder.Update();
    }
}
  • What if there are multiple users deleting items at the same time? – Remko Nov 10 '13 at 21:03
  • With Vardhaman's answer that property will be true forever after a user has first tried to delete item/items. For example if another user tries to delete items he will not get custom warning. That property should be reset after every "action". – Naim Murati Nov 10 '13 at 21:03
  • You are right. The code does not cater to that situation. But this is just some sample code to access the List Property Bag from an Event Receiver and not the complete solution. Parts from this answer can be used to find a solution which bests suits the requirement. – Vardhaman Deshpande Nov 10 '13 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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