2
votes

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

3

3 Answers

2
votes

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.

1
votes

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)

0
votes

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;