Deleting events

Sitefinity allows you to delete events through the Events API.

When deleting an event, you must perform the following:

  1. Get the event.

    First, you must get the master version of the event corresponding to the specified ID.

    For more information about querying events, see Querying events.

    For more information about finding specific events, see Finding events.

  2. Delete the event.

    After you get the master version of the event, you delete it.

  3. Save the changes.

    Finally, you must save the changes.

To delete an event you can use the Native API or the Fluent API.

NOTE: The code examples below work with the ID of the master version of the event. Deleting the master version also deletes the other versions of the item. For more information about deleting items using the ID of the live version, see Deleting content.

Deleting an event by its ID

The following examples delete an event by the ID of its master version.

Native API

public void DeleteEventNativeAPI(Guid masterEventId)
{
    EventsManager eventsManager = EventsManager.GetManager();
 
    // Get the master version of the event
    Event eventItem = eventsManager.GetEvents().Where(ev => ev.Id == masterEventId).FirstOrDefault();
 
    if (eventItem != null)
    {
        // Mark the event to be deleted
        eventsManager.DeleteEvent(eventItem);
 
        // Save the changes
        eventsManager.SaveChanges();
    }
}

First, you initialize the EventsManager. Then, you get the master version of the event using GetEvents and filtering based on the Id property.

For more information about querying events, see Querying events.

For more information about finding specific events, see Finding events.

To delete the event, you call the DeleteEvent method passing the master version as argument. Finally, you save the changes.

Fluent API

public void DeleteEventFluentAPI(Guid masterEventId)
{
    int count = 0;
    App.WorkWith().Events().Where(ev => ev.Id == masterEventId).Count(out count);
 
    if (count > 0)
    {
        App.WorkWith().Event(masterEventId).Delete().SaveChanges();
    }
}

First, you initialize the plural facade of the event using App.WorkWith().Events(). Then, you filter based on the Id property to assure that the event exists.

For more information about querying events, see Querying events.

For more information about finding specific events, see Finding events.

To get the master version of the event, you use the singular facade of the event.

NOTE: If there is no event with the specified Id, Event(masterEventId) throws an exception of type ItemNotFoundException.

To delete the event, you use the Delete method of the facade. Finally, you save the changes.

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK