Monday, September 14, 2009

Event Handler in project Server 2007

Project Server Event facilitate the addition of new business logic by extending
the capabilities of Project Server events to developers. PS 2007 allow us to override most of the pre-event and postevent. For this example we will create a pre publishing event to check whether project name is following some creteria or not.

To create a new event handler, follow these steps:

  1. Open Visual Studio 2005.

  2. Select File, New, Project, and create a new class library. Name it MyPSEvent.

  3. Rename the class Class1 to MYPSHandler.

  4. In the Solution Explorer, right-click the project name and select Add Reference. And add references to Microsoft.Office.Project.Server.Events.Receivers.dll and Microsoft.Office.Project.Server.Library.dll assemblies.

  5. Add following using statement in your class
    System.Diagnostics - This namespace contain Event Log object ,that will be used to log the progress, status, and results in the event handler.
    Microsoft.Office.Project.Server.Events - this namespace contains the base event receiver classes of each Project business object.
    Microsoft.Office.Project.Server.Library - this namespace contains the PSContextInfo class, which holds the UserGuid and UserName properties.

  6. Derive your MYPSHandler from the abstract class ProjectEventReceiver .and Create a method to override the base method for the Publishing event by overriding the onPublishing method.

  7. In this method, you can get the username from the object contextInfo and the project
    name from the Event Arguments object e. In this example, we will check if the project name
    meets the set criteria. If it does not, cancel the publishing event by setting the property e.Cancel to true. You should also log the results of this method.


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using Microsoft.Office.Project.Server.Events;
    using Microsoft.Office.Project.Server.Library;
    using System.Web.Mail;
    namespace MyPSEvent
    {
    public class MYPSHandler : ProjectEventReceiver
    {
    public override void OnPublishing(PSContextInfo
    contextInfo,ProjectPrePublishEventArgs e)
    {
    base.OnPublishing(contextInfo, e);

    //creating object to log results
    //and setting the log source.
    EventLog myLog = new EventLog();
    myLog.Source = “Project Event Handler”;

    //Getting user and project information
    // from event arguments
    // from event arguments
    string userName = contextInfo.UserName.ToString();
    string ProjectName = e.ProjectName.ToString();
    int eventId = 3651;
    string logEntry = “Publishing of project “
    + ProjectName + “started by “ + userName;
    e.Cancel = false;

    //if the project name does not meet
    //the criteria cancel the event.
    if (ProjectName.StartsWith(“Project”))
    {
    myLog.WriteEntry(logEntry,
    EventLogEntryType.Information, eventId);
    }
    else
    {
    logEntry += “\nProject name is invalid.
    The name of all published”
    + “projects must start with ‘Project’”;
    myLog.WriteEntry(logEntry,
    EventLogEntryType.Warning, eventId);
    e.Cancel = true;
    }
    }
    }
    }



  8. Create a strong name key file and Build the solution

  9. Add dll to the global assembly cache.

  10. Register the event hander in Project Server 2007,

    • log on to PWA using an account with administrative permissions. On the left page, select Server Settings and under the Operation Policies section, click Server-Side Event Handler Configuration,

    • In the Events page, scroll down the Events list and click the Project link for the Publishing event. Then click New Event Handler in the Event Handlers grid
      On the Event Handler page, type the following values,
      . Name—event handler name, such as MyPSEvent.
      . Description—event handler description.
      . Assembly Name—Assembly name, version, culture, and public key token
      . Class Name—Fully qualified class name, such as MyPSEvent. MYPSHandler .
      . Order—In case there is more than one event handler, this sets the order in which
      they are invoked.

    • When finished, click Save.



  11. Project Server 2007 adds the Project Server event handler in an asynchronous
    process, and it might take a few seconds or minutes for the Event Handlers grid to
    update after the event handler registers.

  12. Testing the Event Handler - To test the event handler, follow these steps:

    • Under Projects in the left pane, select Proposals and Activities.

    • In the Proposals and Activities page, click New, and then click Proposal.

    • In the New Proposal page, select New and enter a proposal name and description.

    • Click Save, and then click Save and Publish.

    • Open the Event Viewer and select Application in the left pane. You
      should be able to see a warning from the event handler near the top of the list.




  13. Debugging the Event Handler in Visual Studio 2005

    • Attach debugging process – To debug an event handler, you must use Visual Studio 2005 and attach the debugger to the Project Server Eventing process.
      Note – For debugging from a remote computer, install the Microsoft Visual
      Studio Remote Debugging Monitor in the Project Server computer and follow these steps:

      • On the Debug menu, click Attach to Process.
      • select the default transport and browse to the Project Server computer as the qualifier.
      • Select Microsoft Office Project Server Eventing Process and click Attach.




    • After the process attached, Select Tools, Options.

    • Expand the Debugging node and select Symbols.

    • Click the folder icon and copy the path to the debug directory of your project.

    • Select the option Load Symbols Using the Updated Settings When This Dialog Is
      Closed, and click OK.

    • Place a breakpoint at the onPublishing method and trigger the
      Publishing event by publishing a project. The process will stop in your breakpoint and
      you can step through the method.

    • Debugging will not work if the code changes after the event handler has been registered
      in Project Server. If the code changes and is recompiled, you will need to reregister the
      event handle. For this registering the new assembly in the Global Assembly Cache and restart the service (go to the Central Admin- > select Operations ->Services on Server. Stop and restart the “Project Application Service” ). This will register the new assemblies from the Global Assembly Cache in Project Server. If you want to reregister it in production environment where you cant restart the services then Go to the Events page and select the event the event handler was registered to. Select the event handler you want to unregister in the Event Handlers grid and click Delete Event Handler. Register the new Event Handler assembly in the global assembly cache. Register the new event handler in Project Server.



No comments: