Deleting news items

This topic explains how to delete news items. The examples below show you how to delete all of the available news items or how to delete only a specific item by its ID.

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

Deleting a single news item

When deleting a specific news item by the ID of its master version, you must perform the following:

  1. Get the item.

    First, get an instance of the master version with the specified ID.

  2. Delete the item.

    Mark the master version to be deleted and save the changes. 

The following code deletes a news item by the ID of its master version.

Native API

private void DeleteNewsItemNativeAPI(Guid masterNewsId)
{
    NewsManager manager = NewsManager.GetManager();
  
    //Get the master version of the item.
    NewsItem master = manager.GetNewsItems().Where(item => item.Id == masterNewsId).SingleOrDefault();
  
    if (master != null)
    {
        //Mark the item to be deleted.
        manager.Delete(master);
  
        //Save the changes.
        manager.SaveChanges();
    }
}

First, you get an instance of the NewsManager class. Then, you get the master version corresponding to the ID. Then, you mark the news item to be deleted by calling the Delete method of the manager with the master version as an argument. Finally, you save the changes.

Fluent API

private void DeleteNewsItemFluentAPI(Guid masterNewsId)
{
    App.WorkWith().NewsItem(masterNewsId).Delete().SaveChanges();
}

First, you get the singular news item facade of the master version with the specified ID. Then, you call the Delete method to mark the item to be deleted. Finally, you call SaveChanges.

Deleting all news items

When deleting all news items, you must perform the following:

  1. Get the available items.

    Get the master versions of the available items. 

  2. Delete each item in the collection.

    Iterate through the collection and delete each item.

The following code deletes all news items.

Native API

private void DeleteAllNewsItemsNativeAPI()
{
    NewsManager manager = NewsManager.GetManager();
  
    var items = manager.GetNewsItems().Where(item => item.Status == ContentLifecycleStatus.Master).ToList();
  
    foreach (NewsItem item in items)
    {
        manager.Delete(item);
    }
  
    manager.SaveChanges();
}

First, you get an instance of the manager. Then, you get the master versions of the available items. You iterate through the collection and mark each item to be deleted. Finally, you save the changes.

Fluent API

private void DeleteAllNewsItemsFluentAPI()
{
    App.WorkWith().NewsItems().Delete().SaveChanges();
}

First, you get the plural facade for the news item. Then you call the Delete method to mark each item to be deleted. Finally, you save the changes.

See Also

Next steps

+1-888-365-2779
sales@sitefinity.com

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