Email alrts / notifications for any new content?
We have SF project that is basically just a large document repository where different groups have their own folder that only they and their team have access to. When people upload or change documents in their libraries all we would like a small group of owner/admins to receive an email notification. In SharePoint they call this an alert? Simple, but elusive. Possible?
Thanks!
Hello Todd,
One option is to use our EventHub implementation.
EventHub is a centralized place in Sitefinity where you can subscribe for data events. You can find a list of the exposed content-specific events in the system here.
However, in some specific scenarios, you might want to have a global event for all content, you can subscribe to the IDataEvent - it will be thrown on Create, Update, and Delete actions for all content. Inside the event handler you can get information about the:
- Event action ("Created", "Updated", "Deleted")
- item type
- item ID
- item ProviderName
this data is sufficient to allow you to load the corresponding manager and retrieve the actual item.
To wrap it up I've prepared a small sample:
void
Bootstrapper_Initialized(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
if
(e.CommandName ==
"Bootstrapped"
)
EventHub.Subscribe<IDataEvent>(Content_Action);
private
void
Content_Action(IDataEvent @
event
)
var action = @
event
.Action;
var contentType = @
event
.ItemType;
var itemId = @
event
.ItemId;
var providerName = @
event
.ProviderName;
var manager = ManagerBase.GetMappedManager(contentType, providerName);
var item = manager.GetItemOrDefault(contentType, itemId);
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Telerik.Sitefinity.Lifecycle;
using
Telerik.Sitefinity.GenericContent.Model;
using
System.Net.Mail;
using
Telerik.Sitefinity.Libraries.Model;
using
Telerik.Sitefinity.Modules.Newsletters;
using
Telerik.Sitefinity.Security.Claims;
using
Telerik.Sitefinity.Security;
using
Telerik.Sitefinity.Security.Model;
namespace
SitefinityWebApp.Decorators
public
class
CustomDocumentsDecorator : LifecycleDecorator
public
CustomDocumentsDecorator(ILifecycleManager manager, LifecycleItemCopyDelegate copyDelegate,
params
Type[] itemTypes) :
base
(manager, copyDelegate, itemTypes)
public
CustomDocumentsDecorator(ILifecycleManager manager, Action<Content, Content> copyDelegate,
params
Type[] itemTypes)
:
base
(manager, copyDelegate, itemTypes)
protected
override
ILifecycleDataItemGeneric ExecuteOnPublish(ILifecycleDataItemGeneric masterItem, ILifecycleDataItemGeneric liveItem, System.Globalization.CultureInfo culture =
null
, DateTime? publicationDate =
null
)
if
(masterItem
is
Document)
var documentItem = masterItem
as
Document;
var manager = NewslettersManager.GetManager();
var mailingList = manager.GetMailingLists().Where(ml => ml.Title ==
"Admins"
).FirstOrDefault();
if
(mailingList !=
null
&& mailingList.Subscribers().Count() > 0)
var mailMessage =
new
MailMessage();
mailMessage.From =
new
MailAddress(
"sender@server.com"
);
var subscribers = mailingList.Subscribers();
foreach
(var subscriber
in
subscribers)
mailMessage.To.Add(
new
MailAddress(subscriber.Email));
mailMessage.Subject = String.Format(
"Document Item \"0\" Published"
, documentItem.Title.Value);
var user = GetCurrentUser();
string
username =
string
.Empty;
if
(user !=
null
)
username = user.FirstName +
" "
+ user.LastName;
mailMessage.Body =
string
.Format(
"A new document item has been published. Item with title 0 has been updated on 1 by 2"
, documentItem.Title, DateTime.Now, username);
var client =
new
SmtpClient();
client.Host =
"localhost"
;
client.Port = 26;
client.Send(mailMessage);
return
base
.ExecuteOnPublish(masterItem, liveItem, culture, publicationDate);
private
SitefinityProfile GetCurrentUser()
var identity = ClaimsManager.GetCurrentIdentity();
if
(identity !=
null
&& identity.UserId != Guid.Empty)
UserProfileManager profileManager = UserProfileManager.GetManager();
UserManager userManager = UserManager.GetManager(
"Default"
);
User user = userManager.GetUser(identity.UserId);
SitefinityProfile profile =
null
;
if
(user !=
null
)
profile = profileManager.GetUserProfile<SitefinityProfile>(user);
return
profile;
return
null
;
protected
void
Application_Start(
object
sender, EventArgs e)
Bootstrapper.Initialized +=
new
EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized_Decorator);
void
Bootstrapper_Initialized_Decorator(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
ObjectFactory.Container.RegisterType<ILifecycleDecorator, Decorators.CustomDocumentsDecorator>(
typeof
(LibrariesManager).FullName,
new
InjectionConstructor(
new
InjectionParameter<ILifecycleManager>(
null
),
new
InjectionParameter<Action<Content, Content>>(
null
),
new
InjectionParameter<Type[]>(
null
))
);
Thank you so much for this thoughtful and thorough answer. I will be forwarding this to my Sitfinity managers to try and implement. Will report back. Thanks again!
Hi Todd,
You can find more information about our EventHub and event driven development here. More information
about our events system here.
I hope you find this useful.
Regards,
Nikola Zagorchev
Telerik