Note:
- Replace pwaUrl with your project server site collection
- This example shows to how to register and un register deleted event receivers. You can replace with your respective event receiver accordingly.
- You can use same code in feature activating and deactived events to automate your deployment process.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
using
Microsoft.ProjectServer.Client;
using
Microsoft.SharePoint.Client;
using
Microsoft.Office.Project.Server.Library;
private static void RegisterEvent(string pwaUrl)
{
try
{
ProjectContext projContext = new ProjectContext(pwaUrl);
projContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
string cred = projContext.Credentials.ToString();
Console.WriteLine(cred);
projContext.EventHandlers.Add(new EventHandlerCreationInformation()
{
Id = new Guid(),
Name = "Name of
the event receiver",
AssemblyName = "xxxxxxx, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=cb7c82b7e5fdfc41",
ClassName = "xxxxxx.DeleteProjectEvent",
Order = 1,
EventId = (int)PSEventID.ProjectDeleted
//Id = new Guid(),
//Name =
"TestProjectEvents.DeleteProjectEvent",
//AssemblyName = "TestProjectEvents,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=b57676d8dfd71ad5",
//ClassName =
"TestProjectEvents.DeleteProjectEvent",
//Order = 1,
//EventId =
(int)PSEventID.ProjectDeleting
});
projContext.EventHandlers.Update();
projContext.ExecuteQuery();
Console.WriteLine("success registered project event receiver");
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
private static void UnRegisterEvent(string pwaUrl)
{
try
{
ProjectContext projContext = new ProjectContext(pwaUrl);
projContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Guid eventId = new Guid();
projContext.Load(projContext.EventHandlers);
projContext.ExecuteQuery();
foreach (var projEvent in
projContext.EventHandlers)
{
if (projEvent.Name == "MSPSProjectEventHandlers.DeleteProjectEvent")
{
eventId = projEvent.Id;
var eh =
projContext.EventHandlers.GetByGuid(eventId);
projContext.EventHandlers.Remove(eh);
projContext.EventHandlers.Update();
projContext.ExecuteQuery();
Console.WriteLine("Successfully unregistered");
}
//if (projEvent.Name ==
"TestProjectEvents.DeleteProjectEvent")
//{
//
eventId = projEvent.Id;
//
var eh = projContext.EventHandlers.GetByGuid(eventId);
//
projContext.EventHandlers.Remove(eh);
//
projContext.EventHandlers.Update();
//
projContext.ExecuteQuery();
//}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
No comments:
Post a Comment