Last active
December 19, 2015 13:39
-
-
Save rcurlette/5963728 to your computer and use it in GitHub Desktop.
C# Tridion Event System code for sending messages to the MessageCenter using the Signalr4Tridion GUI Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNet.SignalR.Client; | |
using Tridion.ContentManager; | |
using Tridion.ContentManager.ContentManagement; | |
using Tridion.ContentManager.Extensibility; | |
using Tridion.ContentManager.Extensibility.Events; | |
using Tridion.ContentManager.Publishing; | |
namespace EventSystemSignalr | |
{ | |
[TcmExtension("Publish Transaction Notification Event Handler")] | |
public class PublishNotificationHandler : TcmExtension | |
{ | |
public PublishNotificationHandler() | |
{ | |
EventSystem.Subscribe<PublishTransaction, SaveEventArgs>(TransactionSaved, EventPhases.TransactionCommitted); | |
EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaved, EventPhases.TransactionCommitted); | |
} | |
public void TransactionSaved(PublishTransaction transaction, SaveEventArgs args, EventPhases phases) | |
{ | |
string message = GetMessage(transaction); | |
if (!String.IsNullOrEmpty(message)) | |
{ | |
var connection = new Connection("http://localhost:8123/channel"); | |
connection.Start().Wait(); | |
int userId = GetUserId(transaction); | |
connection.Send("join " + userId); | |
connection.Send(userId + ":" + message); | |
connection.Stop(); | |
} | |
} | |
public void ComponentSaved(Component component, SaveEventArgs args, EventPhases phases) | |
{ | |
var connection = new Connection("http://localhost:8123/channel"); | |
connection.Start().Wait(); | |
connection.Send("Component " + component.Title + ", hello from SignalR "); | |
connection.Stop(); | |
} | |
private int GetUserId(PublishTransaction tridionObject) | |
{ | |
TcmUri userUri = tridionObject.Creator.Id; | |
return userUri.ItemId; | |
} | |
private string GetMessage(PublishTransaction transaction) | |
{ | |
string message = ""; | |
if (transaction.State == PublishTransactionState.Success) | |
{ | |
if (transaction.Items.Count == 1) | |
{ | |
message = "Publishing " + transaction.Items[0].Title + " completed."; | |
} | |
} | |
else if (transaction.State == PublishTransactionState.Failed) | |
{ | |
if (transaction.Items.Count == 1) | |
{ | |
message = "Publishing " + transaction.Items[0].Title + " failed."; | |
} | |
} | |
return message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment