# Query events

Sitefinity CMS allows you to query for a specific event by its *ID*. To search for specific events based on any other property or criteria, see [For developers: Find events](https://www.progress.com/documentation/sitefinity-cms/for-developers-find-events).

> info \*\*NOTE:\*\*The code examples below work with the *ID* of the master version of the item and return the live version of the item. For more information about other scenarios, see [For developers: Query master and live versions](https://www.progress.com/documentation/sitefinity-cms/for-developers-query-master-and-live-versions).

## Query a single event

When querying the live version of a specific event by the *ID* of its master version, you must perform the following:

1. Get the event.\
   First, get an instance of the master version of the event that corresponds to the specified *ID*.
2. Get the live version.\
   The instance that corresponds to the *ID* argument is the master version of the event. You can get the live version explicitly.

   > info  **NOTE**: The live version is used only when displaying the event in a front-end scenario. To modify the event, consider using the master version. To transfer the changes to the live version, publish the master version.
3. Return the event.\
   Return the event. If an item with the specified *ID* does not exist, or if it has no live version, you return null.

The following examples query the live version of an event by the *ID* of its master version:

**Native API**

```C#
using System;
using System.Linq;
using Telerik.Sitefinity.Events.Model;
using Telerik.Sitefinity.Modules.Events;

namespace SitefinityWebApp
{
   public class QuerySingleEventNativeAPI
   {
       public Event GetEventByIdNativeAPI(Guid masterEventId)
       {
           EventsManager eventsManager = EventsManager.GetManager();
           Event eventItem = eventsManager.GetEvents().Where(ev => ev.Id == masterEventId).FirstOrDefault();

           if (eventItem != null)
           {
               eventItem = eventsManager.Lifecycle.GetLive(eventItem) as Event;
           }

           return eventItem;
       }
   }
}
```

First, you get an instance of the `EventsManager` class. You get the specified event by querying all items and filtering the collection by the `ID` of the event. If the item exists, you get its live version. If no live version exists (i.e. the event has not been published), null is returned. Finally, you return the event.

To find the event, you can also use the `GetEvent` method passing `masterEventId`:

```C#
using System;
using Telerik.Sitefinity.Events.Model;
using Telerik.Sitefinity.Modules.Events;

namespace SitefinityWebApp
{
   public class QuerySingleEventNativeAPI2
   {
       public Event GetEventByIdNativeAPI(Guid masterEventId)
       {
           EventsManager eventsManager = EventsManager.GetManager();
           Event eventItem = eventsManager.GetEvent(masterEventId);

           return eventItem;
       }
   }
}
```

> info **NOTE**: Calling `GetEvent(masterEventId)` throws an exception of type `ItemNotFoundException`, if there is no event with the specified `Id`.

**Fluent API**

```C#
using System;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Events.Model;

namespace SitefinityWebApp
{
   public class QuerySingleEventFluentAPI
   {
       public Event GetEventByIdFluentAPI(Guid masterEventId)
       {
           int count = 0;
           Event eventItem = null;

           App.WorkWith().Events().Where(ev => ev.Id == masterEventId).Count(out count);

           if (count > 0)
           {
               eventItem = App.WorkWith().Event(masterEventId).GetLive().Get();
           }

           return eventItem;
       }
   }
}
```

First, you use the plural facade of the event to assure that the event with the specified `Id` exists. Then, you use the `GetLive` method of the singular facade to get the instance of the live version. Finally, you return the event.

## Query all events

When querying all events, you must perform the following:

1. Query all events.\
   First, get a query of all available events. The query includes all live, master and temp versions.
2. Filter the query.\
   Filter the query to return only the live versions.
3. \*Get the events.\*Get the filtered events.
4. Return the events.\
   Return the events. If no events exist or there are no any published events, you return an empty collection.

The following code queries all events:

**Native API**

```C#
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity.Events.Model;
using Telerik.Sitefinity.Modules.Events;

namespace SitefinityWebApp
{
   public class QueryAllEventsNativeAPI
   {
       private List<Event> GetAllEventsNativeAPI()
       {
           EventsManager eventsManager = EventsManager.GetManager();
           return eventsManager.GetEvents().Where(ev => ev.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).ToList();
       }
   }
}
```

First, you get an instance of the `EventsManager` class. You query all events, including the live, master and temp versions of each. You filter the collection based on the *Status* property to return only the live versions. Finally, you return the events.

**Fluent API**

```C#
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Events.Model;
using Telerik.Sitefinity.GenericContent.Model;

namespace SitefinityWebApp
{
   public class QueryAllEventsFluentAPI
   {
       private List<Event> GetAllEventsFluentAPI()
       {
           return App.WorkWith().Events().Where(ev => ev.Status == ContentLifecycleStatus.Live).Get().ToList();
       }
   }
}
```

First, you get an instance of the plural facade of the event. Then, you get all events, including the live, master and temp versions of each. You filter the collection based on the `Status` property to return only the live versions. Finally, you return the events.
