Register Remote Event receiver to Project server/Project Online events using CSOM

How to register the custom “Remote event receivers” to the Project Online events.

In this example, assume that I’ve developed remote event receiver for “ProjectPublishing” event and deployed that solution to Azure web app. Now I would required to register that event handler solution with the “ProjectPublishing” event of the project online.

Note: Will write separate blog for Remote event receiver development and deployment to Azure web app.

Before writing the code, make a note of the End point URL of the solution.You can use any type of solutions (i.e. Console/WEB/windows forms) to register the event receiver.

Here, have used web application with Two buttons, one is for “Register” another one is for “Un-Register”.


using Microsoft.Office.Project.Server.Library;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EventHandler=Microsoft.ProjectServer.Client.EventHandler;

namespace DeployRemoteEventReceiver
{
public partial class WebForm1 : System.Web.UI.Page
{

static readonly Guid EventHandlerUid = new Guid("{20d135f3-671d-4fef-a0e1-7bb2d27e5d28}");
static readonly String ProjectServerUrl = "PWA URL"; //PWA instance where you wanted to register the event handler
static readonly String BaseUrl = "https://pjoeventreceivers.azurewebsites.net/PPublishingService.svc"; //END Point URL Where your Remote Event receiver was deployed

string userName = "<<UserName>>";
string password = "<<Password>>";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulatePage();
}
}
private SecureString getSecureString()
{
SecureString sec = new SecureString();
password.ToCharArray().ToList().ForEach(c => sec.AppendChar(c));
sec.MakeReadOnly();
return sec;
}

private void PopulatePage()
{
// Check if our event handler is registered in Project Server. We use CSOM
ProjectContext pc = new ProjectContext(ProjectServerUrl);
pc.Credentials = new SharePointOnlineCredentials(userName, getSecureString());
pc.Load(pc.EventHandlers);
EventHandler evh = pc.EventHandlers.GetByGuid(EventHandlerUid);
pc.Load(evh);
pc.ExecuteQuery();
foreach(EventHandler ev in pc.EventHandlers)
{
Response.Write(ev.Name + ", " + ev.Id + ", " +ev.ClassName + " , " + ev.EndpointUrl);
}

if ((bool)evh.ServerObjectIsNull)
{
// not registered
btnRegister.Enabled = true;
btnUnregister.Enabled = false;
}
else
{
btnUnregister.Enabled = true;
btnRegister.Enabled = false;
}
}

protected void btnRegister_Click(object sender, EventArgs e)
{
ProjectContext pc = new ProjectContext(ProjectServerUrl);
pc.Credentials = new SharePointOnlineCredentials(userName, getSecureString());
pc.EventHandlers.Add(new EventHandlerCreationInformation()
{
EndpointUrl = BaseUrl,
Id = EventHandlerUid,
Name = "Project onPublishing Event",
EventId = (int)PSEventID.ProjectPublishing, //choose the correct event handler here.
Order = 1
});
pc.EventHandlers.Update();
pc.ExecuteQuery();
Response.Redirect("WebForm1.aspx");
}

protected void btnUnregister_Click(object sender, EventArgs e) //un register the event handler
{
ProjectContext pc = new ProjectContext(ProjectServerUrl);
pc.Credentials = new SharePointOnlineCredentials(userName, getSecureString());
pc.Load(pc.EventHandlers);
EventHandler evh = pc.EventHandlers.GetByGuid(EventHandlerUid);
pc.Load(evh);
pc.ExecuteQuery();
pc.EventHandlers.Remove(evh);
pc.EventHandlers.Update();
pc.ExecuteQuery();
Response.Redirect("WebForm1.aspx");
}
}
}