Project Server 2007 events

M

Marc

Hi!
I'ld like to call some code on each creation or saving of a project inside
Project Server 2007. Inside this code I want to access the ProjectDataSet and
its information. In my opinion that's just possible in pre-event handlers,
right? Now my problem is, that a derivate of the class "ProjectEventReceiver"
just provides me the "OnSaved" & "OnCreated" handlers. I also tried to use
the "OnCreating" & "OnUpdating" event handlers, but unfortunately it seems
that they aren't launched on creation and saving of a project. This is
specially surprising, as the "OnCreated" event handler is called, but not the
"OnCreating" handler... Am I doing something wrong or is there a turnaround
to solve my problem?

Thanks in advance.
 
J

Jim Corbin [MSFT]

The event arguments parameter in both pre- and post-event handlers can
include a DataSet for the entity, if the related PSI method uses a DataSet.
For example, the event arguments parameter for the CustomField OnCreated
event handler includes CustomFieldInformation of type CustomFieldDataSet.

To use a DataSet in an event handler, you must set a reference to the
Microsoft.Office.Project.Schema.dll assembly. The
Microsoft.Office.Project.Server.Schema namespace includes the necessary
class definitions such as CustomFieldDataSet.

You can copy the schema assembly from
C:\WINDOWS\assembly\GAC_MSIL\Microsoft.Office.Project.Schema\12.0.0.0__71e9bce111e9429c
if you first run the following command:
regsvr32 /u C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll
--then re-register shfusion.dll when you're done.

Datasets in event handlers are for read-only use. For example, you cannot
modify the CustomFieldDataSet to change the name of a custom field within
the OnCreating pre-event handler. However, you can trap the event, get the
properties you need, cancel creation of the custom field, and then use the
CreateCustomFields method in the CustomFields Web service to create a new
custom field with the correct name.
Here's some code (modified from the SDK example) that uses a
CustomFieldDataSet in a post-event handler.

using System;
using System.Diagnostics;
using System.Data;
using System.Xml;
using Microsoft.Office.Project.Server.Events;
using PSLibrary = Microsoft.Office.Project.Server.Library;

namespace TestEventHandlers
{
public override void OnCreated(
PSLibrary.PSContextInfo contextInfo,
CustomFieldsPostEventArgs e)
{
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "Custom Field OnCreated Event Handler";

// Get information from the event arguments, and
// write an entry to the Application event log.
string userName = contextInfo.UserName.ToString();

string cfName =
e.CustomFieldInformation.CustomFields.Rows[0]["MD_PROP_NAME"].ToString();
byte cfTypeEnum =
(byte)e.CustomFieldInformation.CustomFields.Rows[0]["MD_PROP_TYPE_ENUM"];
Guid cfUid =
(Guid)e.CustomFieldInformation.CustomFields.Rows[0]["MD_PROP_UID"];

string[] cfTypes =
Enum.GetNames(typeof(PSLibrary.CustomField.Type));

string cfType = "";
int index = 0;
e.CustomFieldInformation;
foreach (int i in
Enum.GetValues(typeof(PSLibrary.CustomField.Type)))
{
if (i == Convert.ToInt32(cfTypeEnum))
{
cfType = cfTypes[index];
break;
}
index++;
}

int eventId = 3652;
string logEntry;

logEntry = "User: " + userName +
"\nCustom Field Name: " + cfName +
"\nGUID: " + cfUid.ToString() +
"\nType: " + cfType +
"\nThe custom field has been created.";

myLog.WriteEntry(logEntry, EventLogEntryType.Information,
eventId);
}
}

When you create a class that inherits from ProjectEventReceiver, and crtete
a method within it, you should see all of the pre- and post-events within
the Intellisense list (for the list of events, see ProjectEventReceiver
Methods (Microsoft.Office.Project.Server.Events) --
http://msdn2.microsoft.com/en-us/li...rver.events.projecteventreceiver_methods.aspx -
- in the SDK. If you don't, check whether you've set a reference to
Microsoft.Office.Project.Server.Events, and included it in the using
statements:
using Microsoft.Office.Project.Server.Events;

Whether an event is fired, depends on the situation. If you're using WinProj
Pro, you have to save to get the OnCreating event. Another possibility is
that the event handler isn't registered properly. You need the exact
Namespace.ClassName, and the correct assembly name, e.g.
TestEventHandler,Version=1.0.0.0,Culture=Neutral,PublicKeyToken=1e3db2b86a6e0210

--Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top