0

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!

0

You will need to separately associate the Add, Delete and Update event receivers to the list, by passing appropriate parametric values to the code below -

list.EventReceivers.Add

The Add method for event receiver takes following parameters -

public void Add(SPEventReceiverType receiverType, string assembly, string className);

Note - Adding event receiver to the list is one time activity. Once you have event receiver properly attached it will fire for every time for the action.

Take care not to attach an event receiver twice/more or your code will run as many times as it is attached.

0

I was wrong. This EventReceiver are firing correctly - It was only a debug mistake from myself.

1.) Where is the HTA dialog?

  • Services do not have GUI. This were a stupid idea from myself;

2.) But, the ".ctor" HTA dialog was showed as well.

  • I think its because the e.Update(); line was being executed by the Console Application process in an Visual Studio Debug session, not the w3wp or a SharePoint process;

3.) So, what was wrong with my original implementation?

  • I am using the System.Web.MVC and for some reason it weren't findable by SharePoint. I added it to GAC and works fine.

NOTE: All this bad practices of "add this and that to GAC" are just for development. Im going to do a elegant feature solution :)

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.