Last active
August 29, 2015 14:16
-
-
Save hardye/38d827b3753bdde0a0e5 to your computer and use it in GitHub Desktop.
Sitefinity IDataEvent sample
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 Telerik.Sitefinity.Abstractions; | |
using Telerik.Sitefinity.Data.Events; | |
using Telerik.Sitefinity.Events.Model; | |
using Telerik.Sitefinity.Scheduling; | |
using Telerik.Sitefinity.Services; | |
namespace MyApp.MyNamespace | |
{ | |
public class Setup | |
{ | |
public static void PreApplicationStart() | |
{ | |
Bootstrapper.Initialized += Bootstrapper_Initialized; | |
} | |
private static void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e) | |
{ | |
if (e.CommandName == "Bootstrapped") | |
{ | |
EventHub.Subscribe<IDataEvent>(HandleDataEvent); | |
} | |
} | |
private static void HandleDataEvent(IDataEvent evt) | |
{ | |
// Only process user-initiated changes | |
if (!String.IsNullOrWhiteSpace(evt.Origin)) | |
{ | |
return; | |
} | |
ILifecycleEvent levt = evt as ILifecycleEvent; | |
if (levt == null || levt.Status.Equals("Master", StringComparison.InvariantCultureIgnoreCase) == false) | |
{ | |
return; | |
} | |
// Both new and edited items raise an "updated" event | |
if (evt.Action.Equals("Updated", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
Type itemType = evt.ItemType; | |
if (itemType == typeof(Event)) // <- Filter for your desired content type here | |
{ | |
/* Something needs to be done in response to the "updated" event. Schedule a task to do this. */ | |
MyCustomTask.TaskModel model = new MyCustomTask.TaskModel | |
{ | |
ItemId = evt.ItemId, | |
ProviderName = evt.ProviderName | |
}; | |
// Start the task 2 seconds from now. | |
MyCustomTask task = new MyCustomTask(model) | |
{ | |
Key = evt.ItemId.ToString(), | |
ExecuteTime = DateTime.UtcNow.AddSeconds(2) | |
}; | |
SchedulingManager schedulingManager = SchedulingManager.GetManager(); | |
schedulingManager.AddTask(task); | |
schedulingManager.SaveChanges(); | |
} | |
} | |
} | |
} | |
} |
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 System.Linq; | |
using ServiceStack; | |
using Telerik.OpenAccess; | |
using Telerik.Sitefinity.Data; | |
using Telerik.Sitefinity.Model; | |
using Telerik.Sitefinity.Scheduling; | |
namespace MyApp.MyNamespace | |
{ | |
/// <summary> | |
/// Performs the work in response to the IDataEvent. | |
/// </summary> | |
class MyCustomTask : ScheduledTask | |
{ | |
public MyCustomTask() | |
{ | |
} | |
public MyCustomTask(TaskModel model) | |
{ | |
Model = model; | |
} | |
public override void ExecuteTask() | |
{ | |
// Do your actual work here. | |
// Make sure to call manager.SaveChanges() with a custom string parameter, | |
// so you don't trigger your event handler again. Like this: | |
... | |
myManager.SaveChanges(GetType().ToString()) | |
} | |
public override string TaskName | |
{ | |
get { return GetType().FullName; } | |
} | |
public override string GetCustomData() | |
{ | |
return Model == null ? null : Model.ToJsv(); | |
} | |
public override void SetCustomData(string customData) | |
{ | |
if (!String.IsNullOrWhiteSpace(customData)) | |
{ | |
Model = customData.FromJsv<EventModel>(); | |
} | |
} | |
/// <summary> | |
/// Gets or sets the task's payload. | |
/// </summary> | |
public TaskModel Model { get; set; } | |
/// <summary> | |
/// Holds the data required to execute a <see cref="MyCustomTask"/>. | |
/// </summary> | |
public class TaskModel | |
{ | |
/// <summary> | |
/// Gets or sets the item's (master) ID. | |
/// </summary> | |
public Guid ItemId { get; set; } | |
/// <summary> | |
/// Gets or sets the provider name. | |
/// </summary> | |
public string ProviderName { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment