I want to programmatically add an EventReceiver
to some lists.
I have a signed assembly named Customer.Services.Sharepoint
where my TestSPItemEventReceiver
resides.
For debug purpose, it only shows a HTA dialog when fired:
public class TestSPItemEventReceiver : SPItemEventReceiver {
public TestSPItemEventReceiver() { ShowDialogHTA(); }
public override void ItemDeleted (SPItemEventProperties properties) { ShowDialogHTA(); }
public override void ItemAdded (SPItemEventProperties properties) { ShowDialogHTA(); }
public override void ItemUpdated (SPItemEventProperties properties) { ShowDialogHTA(); }
private static void ShowDialogHTA([CallerMemberName]string message = null) {
Process.Start("mshta", "\"javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'TestSPItemEventReceiver', 10, 'Debugging', 64 );close()\"");
}
}
[*] The code that I am using to attach my TestSPItemEventReceiver
is the follow:
using (var web = new SPSite("http://customer/").OpenWeb()) {
web.AllowUnsafeUpdates = true;
var list = web.Lists["Documents"];
var e = list.EventReceivers.Add(Guid.NewGuid());
e.Name = "Test_ALL_" + DateTime.Now.ToString("hh-mm-ss");
e.Type =
SPEventReceiverType.ItemAdded |
SPEventReceiverType.ItemDeleted |
SPEventReceiverType.ItemUpdated; // [**]
e.Assembly = typeof(TestSPItemEventReceiver).Assembly.FullName;
e.Class = typeof(TestSPItemEventReceiver).FullName;
e.SequenceNumber = 10;
e.Update();
list.Update();
web.Update();
web.AllowUnsafeUpdates = false;
}
Now, my steps to do that are:
1.) >_ gacutil /i "path\to\Customer.Services.Sharepoint.dll"
2.) >_ IISRESET
3.) >_ net stop SPTimerV4
4.) >_ net start SPTimerV4
5.) Run the [*] code
After these steps, specifically at e.Update();
code in [*], my ".ctor"
HTA dialog pops up - and it makes me happy! :)
Then, I go to Documents library, like an user and do some additions, deletions and updates, but, TestSPItemEventReceiver
are never called again! - and it makes me sad! :(
Additional info:
My evidence that TestSPItemEventReceiver
were correctly attached is follow PowerShell script...
$web = Get-SPWeb "http://customer/"
$list = $web.Lists["Documents"]
$list.EventReceivers | select Name, Type
...and getting the result:
Name Type
---- ----
Test_ALL_10-07-23 ItemDeleted [***]
I think SPEventReceiverType
is not expected to be used with bitwise operations [* *] , because of [***]. Anyway, at least I expect that the ItemDeleted event beeing triggered.
Finally, my question: What am I doing wrong???
EDIT:
As we can see ItemAdded
, ItemUpdated
and ItemDeleted
, has values 10001, 10002 and 10003. As 10001 | 10002 | 10003 = 10003
my assumption that ItemDeleted must be triggered are right!