2

I have the following scenario in my SharePoint 2007 project. I have timer job definition which each 30 minutes updates SharePoint list from the SQL database table.

I also have added item event handler for my list, so it does not allow users to add new items to the list from SharePoint user interface. In other words, new list items can be added only in timer job definition via SharePoint object model and it should not allow to add new item from UI.

My problem is - Event handler is being executed even when it adds new item from timer job definition, can i somehow check in event handler how the item has been added(via object model or via user interface).

Many Thanks, Hrayr

Hrayrd
  • 61
  • 1
  • 3
  • 6

3 Answers3

2

Couple of options for you.

Flag inserts from your timer job

  • Set a flag in your list, either a column like "AddedFromTimerJob" (can make hidden from UI) or add something into SPListItem.Properties
  • In your event receiver check for the flag/property - if its present let it through, if not then reject.

or Disable the event receiver for the thread, do your insert, re-enable event receiver.

Ryan
  • 23,871
  • 24
  • 86
  • 132
  • Thank you for your reply! Yes, i also though about the flags and disabling event handler, but there is big issue in that case, for example my timer job process 100s of rows and it takes long time to execute it, if i set some flag or disable the event , then during that time if somebody tries to add/update item through UI event handler will not get fired. I was thinking maybe there is some property in SPItemEventProperties which can point us where the event fired from – Hrayrd Jul 20 '11 at 12:17
  • The flag isn't disabling it, just letting your receiver accept it, so no problem. According to that 2nd link the code provided disables it for the inserting thread only (i.e. a single request) and reenables it after - so again no problem. – Ryan Jul 20 '11 at 14:20
  • Ryan,Thanks you very much again! The last question, just to be sure i got it correctly. So the link you provided - if i disable it before some process which takes lets say 3 minutes to execute and adds lots of new items, then start it when it is finished,,, in this case if in that 3 minutes somebody from the UI will try to add new item to that list event will be fired OK? Am i right? It will be great solution if i got it correctly! – Hrayrd Jul 20 '11 at 15:45
  • Thats how I read code and comments - I've not tried it to verify though. – Ryan Jul 21 '11 at 07:57
1

First of all why not set permissions that don't allow adding new items rather than providing user with an error via eventreceiver after he tries to add the item to the list?

About the problem - it is possible to temporary disable event firing. For example, when your timer job runs, disable event firing until you have finished. However it may not be suitable for all situations as it would disable ALL event receivers for list. But it works if you don't have any event recievers that do something to added data on ItemAdding/ItemAdded events.

Code for disabling event firing temporary can be fond here: Disable Sharepoint item events firing during item update (it applies to any event, not just update)

Janis Veinbergs
  • 6,907
  • 5
  • 48
  • 78
  • Is not "disable event firing" only from the event receiver? Anyway, it sounds to me like using the permission scheme is the way to go unless there is some prohibiting circumstance – oglester Jul 21 '11 at 19:41
  • @Greg, the permission scheme will prevent users to add items. But regarding to event receiver problem, it's true that you can only disable event firing from event receiver. But [event receiver enabled/disabled state is set per session (thread)](http://blogs.syrinx.com/blogs/sharepoint/archive/2008/11/10/disabling-event-firing-on-splistitem-update-differently.aspx), not per event receiver. So we workaround creating a class that inherits SPItemEventReceiver and turn off (all) the event receivers outside event receivers :) – Janis Veinbergs Jul 22 '11 at 07:52
0

You can check the user account who is performing the item-add operation.

If System.Threading.Thread.CurrentPrincipal.Identity is the account which runs timer job,set SPEventPropertiesBase.Cancel =false;

Amit Kumawat
  • 186
  • 3