none
Item deleting Event Receiver

    Pergunta

  • Hello,

    I have one calendar list, and it has workflow which starts on "create item" and checks whether it's in given date interval. If it is, item must be deleted, and workflow is doing that without problems. Then massage should inform user about deleting and reason for doing that. As it cannot be done with workflow in SharePoint Designer (at least I don't know how) I decided to make ItemDeleting Event Receiver. Just to check whether it's going to work i wrote this simple code to prevent workflow from deleting item.

           public override void ItemDeleting(SPItemEventProperties properties)
           {
               base.ItemDeleting(properties);
               SPWeb web = properties.OpenWeb();
               SPList list = properties.List;
               SPItem item = properties.ListItem;
    
               properties.Status = SPEventReceiverStatus.CancelWithError;
               properties.Cancel = true;
               properties.ErrorMessage = "Wassuup!??!";
           }

    Well, nothing happens. Workflow delete item, and event receiver doesn't popup message. Can anyone tell me what am I doing wrong? Solution is sandbox, and status is activated. No errors in deploying.

    And one more question. As there is only CancelWithError status, and I actually don't want to cancel deleting, just to inform user about it, can I just write simple alert javascript and fire it somehow with event receiver?

    Thanks in advance!

    sexta-feira, 2 de março de 2012 15:00

Todas as Respostas

  • You can't inform the user of anything during a Workflow,

    Workflows run in the owstimer (sharepoint service timer), and they run asynchronously in there own context.  They canno't pop up messages, or errors of any kind for that matter.

    If you need to deliver a message, you'll most likely need to do it via email, send the user an email with the information.

    You could also create an Alert Message list, e.g. a List with a title column that stores messages.  Then build an Ajax Refreshing webpart that displays messages from that list.  Then have your workflow store a message in that list when it deletes items etc.  However, the user would only see said message if they are viewing a page with that webpart on it at the time of the workflow deleting the item.


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction

    sexta-feira, 2 de março de 2012 15:06
  • That's now very good solution to my problem. If workflow deletes item, and than finish, can I use ItemDeleted Event Receiver to do some logic?
    sexta-feira, 2 de março de 2012 17:18
  • Rather than having an updating event receiver that deletes it if it's in a bad range, you should have the update event receiver just not add the item at all if it's in the date range.  Use the itemUpdating event, and if the item shouldn't be added then set the cancel to be true and set the error page/message such that the message gets to the user.
    sexta-feira, 2 de março de 2012 17:38
  • You can't inform the user of anything during a Workflow,

    Workflows run in the owstimer (sharepoint service timer), and they run asynchronously in there own context.  They canno't pop up messages, or errors of any kind for that matter.

    If you need to deliver a message, you'll most likely need to do it via email, send the user an email with the information.

    You could also create an Alert Message list, e.g. a List with a title column that stores messages.  Then build an Ajax Refreshing webpart that displays messages from that list.  Then have your workflow store a message in that list when it deletes items etc.  However, the user would only see said message if they are viewing a page with that webpart on it at the time of the workflow deleting the item.


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction

    All of that's true for the asynchronous events, but not for the synchronous events.  I.e. it's true for itemUpdated, but not itemUpdating.
    sexta-feira, 2 de março de 2012 17:38
  • What I meant by my post is this scenario,

    1. You have a workflow adding an item to a list, or updating an item in the list.

     That workflow runs,

    It causes the ItemUpdating event to Fire in the 'Context' of the workflow (the owstimer that's running the workflow).

    Even if it's the ItemUpdating event, there is No SPContext.Current in a workflow, which means there is no HttpContext.Current, which means you can't register some script to popup on the page.  It's not related to a any context or a user actually browsing the site, theres no context to use to display something to anyone.

    Even if a user is on the site and clicks something that causes that workflow to run, the workflow is still unaware of that context.  It knows what list it's working with, and where that list is, but it's not aware of that users HttpContext, or Request.

    In short, to display a message to the user it would have to be Outside of the context of a workflow.

    If the user Clicks "Add New Item" and then Item Updating Event did an HttpContext.Current to get the current page and register some javascript to do an alert, that would work.

    But if the workflow added the item, HttpContext.Current would be null and it would fail.

    Also, if you have the event receiver "ItemUpdating" cancel adding the item to the list, and display an error message, you won't see the error message.  Because the workflow context got the error message, not the users HttpRequest context.  In that scenario the error message would only be visible in the ULS log and the Workflow History logs.


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction

    sexta-feira, 2 de março de 2012 19:38
  • I tried to make Item Updating Event Receiver that will check date interval and if item is in that interval it will cancel updating. At first I created sandboxed solution but I was getting error:

    The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request.

    After many fail attempts to fix it, I just created farm solution. Well it doesn't work at all. When I update item nothing happens. Can anyone help me with this?
    Here's the code:

           public override void ItemUpdating(SPItemEventProperties properties)
           {
               SPWeb web = properties.OpenWeb();
               SPList list = properties.List;
    
               if (list.Title == "Kalendar")
               {
                   SPListItem listItem = properties.ListItem;
                   properties.Status = SPEventReceiverStatus.CancelWithError;
                   properties.Cancel = true;
                   properties.ErrorMessage = "Not Allowed!";
               }
           }


    terça-feira, 6 de março de 2012 12:03
  • hi!

    why you dont use the ItemAdding event? put the date check code inside and if its not okay

    set the properties.status to cancelwitherror, set the errormessage and cancel to true.

    thats enough to prevent the saving of a calendar item you dont want.

    no need for a workflow and user gets a correct error message.

    br,

    ronald

    terça-feira, 6 de março de 2012 19:25
  • I cannot use IdemAdding event, because date check must bi triggered on item changed as well. Item Updating event should do the trick, but for some reason it doesn't want to work. I wrote details of error in upper post.
    quarta-feira, 7 de março de 2012 08:44
  • then use the code on ItemAdding and ItemUpdating event

    but i think the error is the bigger problem and prevent the receiver from working:

    The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request.

    i found following on a blog of msdn maybe it help to find the problem.

    http://blogs.msdn.com/b/sharepointdev/archive/2011/02/08/error-the-sandboxed-code-execution-request-was-refused-because-the-sandboxed-code-host-service-was-too-busy-to-handle-the-request.aspx

    quarta-feira, 7 de março de 2012 09:00
  • Already gone trough all that solutions. None of them worked. No idea how to fix it.
    quarta-feira, 7 de março de 2012 09:16
  • Hello,


    Something is bothering me with your code, how do you attach your event to your list? (asking that because i don't understand why you putted  "

    if (list.Title == "Kalendar")" )

    Are you able to follow your code action step by step? any error occured?


    Best regards, Christopher. Blog French Blog English

    quarta-feira, 7 de março de 2012 13:52
    Usuário que responde