none
How to create Project Event Handlers for Catching multiple events? RRS feed

  • Question

  • Hi,

    I need to create event receivers in MS Project 2016 where i need to accomplish the below:

    1. Provide access read/write to Team Leads only to the tasks under the summary task where the Custom Field of Summary Task is Team leads Department.

    2. Also, i should restrict the Team lead from pubishing the plan to server if he updates the plan and keep the plan in checked out to Team lead till i force checkin the plan using PSI on Project Manager approval. 

    FYI, The Project manger will check the updates in plan from separate UI and approve/reject the changes to plan. (This will be handled by Code thru a Custom application at a later stage)

    To achieve the above, I am currently trying to create Project Server Event handlers.

    Below is a Sample Code which i have written so far:

    namespace ProjectEvents
    {
        public class EVProject : ProjectEventReceiver
        {
            public override void OnPublishing(PSContextInfo contextInfo, ProjectPrePublishEventArgs e)
            {
                if (e.ProjectName.ToLower().Contains("test")) 
                {
                    try
                    {

                       {
                            e.CancelReason = "test-scheduleEvents" + " " + contextInfo.UserName;
                            e.Cancel = true;
                        }
                    }
                    catch (Exception er)
                    {
                        string error = er.Message;
                    }
                }
                else
                {

                }
            }
        }
        public class EVUpdate : ProjectEventReceiver
        {
            public override void OnUpdating(PSContextInfo contextInfo, ProjectPreEventArgs e)
            {
                DataSet ds = e.ProjectDataSet.GetChanges(DataRowState.Modified | DataRowState.Added);
                if (e.ProjectName.ToLower().Contains("testcalendar")) 
                {
                    e.CancelReason = "CancelUpdate";
                    e.Cancel = true;
                }
            }
        }

    }

    The code above i have created different classes for each project server event. i have added both events separately to project server Event Handlers Publishing and updating event in central admin.

    now when i debug only the Publishing class gets called and the updating class never gets called.

    Can anyone let me know the problem with my code?

    Also, I need to know how to compare the Project data in Reporting and Draft in Project server events.

    Regards

    Agasthya


    agasthya

    Tuesday, March 20, 2018 2:51 PM

All replies

  • Hi Agasthya!

    I don't see, why you need two classes for the two events.

    It is a best practice to embed all of your custom code in the event recievers in a try-catch block, and log / trace the entry and exit points (both success and failure), as described in this old post of mine for SharePoint event receivers:

    https://pholpar.wordpress.com/2010/02/15/my-event-receiver-doesnt-fire/

    Thus can you be sure, if the event receiver is invoked or not.

    I assume, you've attached the debugger to Microsoft Project Server Events Service (Microsoft.Office.Project.Server.Eventing.exe).

    Furhtermore, I suggest you to read the hints from this thread as well (if you happened already not to do so):

    1. Re-start the event Service from Services Console after registering the event from central admin (Note from Peter Holpar: you should restart the Event Service after each deployment as well, and re-attach the debugger as well!)
    2. Open the source in visual studio and attach it to “Project Server Eventing service”
    3. On the Tools menu, click Options. Expand the Debugging node in the options list and click Symbols. Click the folder icon and paste the project debug build(location with the PDB file name) in the new symbol file location. Check Load symbols using the updated settings when this dialog is closed to load the symbol file, and then click OK.
    4. In the Code window add a breakpoint during the beginning of the code (note: If the breakpoint shows only a red outline with a yellow warning symbol, the pdb symbol file is not loaded.)
    5. Trigger the event from pwa and see if the break point is hit

    Regards,
    Peter


    • Edited by Peter Holpar Wednesday, March 28, 2018 1:16 PM adding new hints
    Monday, March 26, 2018 2:05 PM
  • Hi,

    I have requirement where I need to perfrom few checks when a task is added/deleted/updated in MPP or PWA. But none of the events (OnUpdating,OnActivityupgrading,OnUpdatingScheduledProject,

    OnEntitiesDeleting) are fired in my code.

    Any idea what the problem could be?

    FYI, I am using Project Server 2016.


    Regards

    Agasthya


    Thursday, April 26, 2018 11:11 AM
  • Hi Peter,

    All your tips were handy for me to understand how to debug the Project Server Events.

    From my Debugging I understood the below steps are crucial for Debugging Events in Project Server,

    1. Build the latest Event receivers dll's

    2. Add the dll's to GAC

    3. Attach the Events in Server Event Handlers in Central Admin ( This once done with correct Assembly name and Class name need not be modified each time we update the Event receivers code with same Strong Key. But if you change the Key in your code then update is reqd.)

    4. Restart the Events Service from Services.msc ( This is mandatory each time a new dll is deployed to GAC else the changes don't take place and debugging doesn't work).

    5. Attach the w3wp process and Microsoft.Office.Project.Server.Eventing

    Now after the above 5 steps you should be able to debug your events.

    Note: Even after the above 5 steps debugging points sometimes show that it wont be hit but when you work on the Project the events still get triggered and debug point are hit.

    I know the above point are very similar to what Peter has given. The only difference is Loading of Symbols is not necessary as Visual studio actualy does the loading of symbols by itself. And if visual studio fails to do it one can try loading the symbols and then trying to debug their code.

    Hope this helps someone.

    Regards

    Agasthya


    agasthya

    Wednesday, May 9, 2018 5:21 AM