The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
protected
void
Application_Start(
object
sender, EventArgs e)
{
Bootstrapper.Initialized += Bootstrapper_Initialized;
}
void
Bootstrapper_Initialized(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
EventHub.Subscribe<IDynamicContentCreatedEvent>(ItemCreatedhandler);
}
public
void
ItemCreatedhandler(IDynamicContentCreatedEvent eventInfo)
{
DynamicContent book = eventInfo.Item;
//The event is fired several times when an item is published though the backend so we need to get it exactly when it is being published
//And we also need only specific items - this time "Books"
if
(book.ApprovalWorkflowState ==
"Published"
&& book.GetType().ToString() ==
"Telerik.Sitefinity.DynamicTypes.Model.Library.Book"
)
{
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type authorType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Library.Author"
);
//Get ids of the related authors
var authors = book.GetValue<Guid[]>(
"Authors"
);
// This is how we get the authors DynamicContent items by their IDs
var authorItems = dynamicModuleManager.GetDataItems(authorType).Where(i => authors.Contains(i.Id));
// Now for every author item we update its books field with the new book if it is not present already
foreach
(var author
in
authorItems)
{
var books = author.GetValue<Guid[]>(
"Books"
).ToList();
if
(!books.Contains(book.OriginalContentId))
{
books.Add(book.OriginalContentId);
}
author.SetValue(
"Books"
, books.ToArray<Guid>());
// This is where changes are persisted to the database. If you are having performance problems SaveChanges can be called once per several items.
dynamicModuleManager.SaveChanges();
}
}
}
Stoimen Stoimenov is Senior QA Engineer in Sitefinity Division. He is responsible for the overall quality of the product and leading various QA tasks in the team.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.