One event handler and multiple PWA sites – Where did that event come from?

An interesting support case came in through our EMEA team (thanks Valdemar) where a customer had a single event handler that was registered in different sites – but when the event was raised they needed to know which site fired it – so they could make the necessary updates against the right site.  In this case they were updating the list of projects in different categories – both on the publishing and check-in events.

So, how do you find out?  The event itself has a parameter of contextInfo and one of the properties that gets set for events is the SiteGuid – which is actually the SiteId of the WSS site hosting PWA.  So this can identify the instance of PWA.  The next step to getting a URL rather than the GUID is to create a new instance of SPSite using this GUID and then simply read the URL property.  So a full event handler that has the extra reference to the required SharePoint library and writes the Project name, the Site ID and the Project Server URL out to the event log would look like this:-

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;

using System.Text;
using Microsoft.Office.Project.Server.Events;
using Microsoft.Office.Project.Server.Library;
using WSSDll = Microsoft.SharePoint;

namespace TestEventHandler
    public class MyEventHandler: ProjectEventReceiver
        public override void  OnPublishing(PSContextInfo contextInfo, ProjectPrePublishEventArgs e)
          base.OnPublishing(contextInfo, e);
            EventLog myLog = new EventLog();
            myLog.Source = “Project Event Handler”;

            // Get information from the event arguments, and
            // write an entry to the Application event log.
            string projectName = e.ProjectName.ToString();
            Guid siteGuid = contextInfo.SiteGuid;
            string pwaUrl = new WSSDll.SPSite(siteGuid).Url;
            int eventId = 3652;
            string logEntry;

            logEntry = “Project: ” + projectName +
                        “nSiteId ” + siteGuid.ToString() +
                        “nPWA Instance: ” + pwaUrl;
            myLog.WriteEntry(logEntry, EventLogEntryType.Information, eventId);


Not very useful in that state – but you can add some extra stuff to do some real work.  They key thing is you don’t have to deploy different event handlers for each instance of PWA – just deploy once and then register in each of the PWA instances you want to use it.  And no need to modify your event handler just because you add a new PWA site to your system.  See the Project Server 2007 SDK for an end-to-end explanation of creating and debugging Project Server 2007 event handlers.


Technorati Tags: