Delete content
When deleting a content item, you must delete its master version. This way the every other version of the item will be deleted too. To delete an item, you must perform the following:
-
Get the master version.
Get the master version of the content item.
-
Delete the master version.
Delete the master version. All other lifecycle versions will be deleted too.
This topic explains how to delete a content item by its ID.
You can perform these actions by using either Native API or Fluent API. Therefore, the sections below provide code snippets for both APIs.
NOTE: In the following examples, news items will be used as content. The code can also be applied to blog posts, list items, events, images, documents, and videos.
Delete an item by the ID of its master version
The following code deletes an item by the ID of its master version.
Native API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Linq; |
|
using Telerik.Sitefinity.Modules.News; |
|
using Telerik.Sitefinity.News.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class DeleteContent_DeleteItemByMasterIDNativeAPI |
|
{ |
|
private void DeleteItemByMasterIDNativeAPI(Guid masterId) |
|
{ |
|
NewsManager manager = NewsManager.GetManager(); |
|
|
|
//Get the master version of the item. |
|
NewsItem master = manager.GetNewsItems().Where(item => item.Id == masterId).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 manager. Then, you get the master version with the specified ID. Then, you mark the 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using Telerik.Sitefinity; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class DeleteContent_DeleteItemByMasterIDFluentAPI |
|
{ |
|
private void DeleteItemByMasterIDFluentAPI(Guid masterId) |
|
{ |
|
App.WorkWith().NewsItem(masterId).Delete().SaveChanges(); |
|
} |
|
|
|
} |
|
} |
First, you get the singular 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
.
Delete an item by the ID of its live version
The following code deletes an item by the ID of its live version.
Native API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Linq; |
|
using Telerik.Sitefinity.Modules.News; |
|
using Telerik.Sitefinity.News.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class DeleteContent_DeleteItemByLiveIDNativeAPI |
|
{ |
|
private void DeleteItemByLiveIDNativeAPI(Guid liveId) |
|
{ |
|
NewsManager manager = NewsManager.GetManager(); |
|
|
|
//Get the live version of the item. |
|
NewsItem live = manager.GetNewsItems().Where(item => item.Id == liveId).SingleOrDefault(); |
|
|
|
if (live != null) |
|
{ |
|
//Get the master version. |
|
NewsItem master = manager.Lifecycle.GetMaster(live) as NewsItem; |
|
|
|
//Mark the item to be deleted. |
|
manager.Delete(master); |
|
|
|
//Save the changes. |
|
manager.SaveChanges(); |
|
} |
|
} |
|
} |
|
} |
First, you get an instance of the manager. Then, you get the live version with the specified ID. Then, you get the master version by calling Lifecycle.GetMaster
with the live version as an argument. Then, you mark the 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System; |
|
using System.Linq; |
|
using Telerik.Sitefinity; |
|
using Telerik.Sitefinity.News.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class DeleteContent_DeleteItemByLiveIDFluentAPI |
|
{ |
|
private void DeleteItemByLiveIDFluentAPI(Guid liveId) |
|
{ |
|
//Get the live version. |
|
NewsItem live = App.WorkWith().NewsItems().Where(item => item.Id == liveId).Get().SingleOrDefault(); |
|
|
|
if (live != null) |
|
{ |
|
//Get the Id of the master version. |
|
Guid masterId = App.WorkWith().NewsItem().GetManager().GetMaster(live).Id; |
|
|
|
//Delete the item. |
|
App.WorkWith().NewsItem(masterId).Delete().SaveChanges(); |
|
} |
|
} |
|
} |
|
} |
First, you get the instance of the live version with the specified ID. Then, you get the ID of the master version by using the manager that is exposed by the singular facade. The you get the singular facade for the master version. Then, to mark the item to be deleted, you call the Delete
method. Finally, you call SaveChanges
.
Delete all items
The following code deletes all of the items.
Native API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using System.Linq; |
|
using Telerik.Sitefinity.GenericContent.Model; |
|
using Telerik.Sitefinity.Modules.News; |
|
using Telerik.Sitefinity.News.Model; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class DeleteContent_DeleteAllItemsNativeAPI |
|
{ |
|
private void DeleteAllItemsNativeAPI() |
|
{ |
|
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
using Telerik.Sitefinity; |
|
|
|
namespace SitefinityWebApp |
|
{ |
|
public class DeleteContent_DeleteAllItemsFluentAPI |
|
{ |
|
private void DeleteAllItemsFluentAPI() |
|
{ |
|
App.WorkWith().NewsItems().Delete().SaveChanges(); |
|
} |
|
} |
|
} |
First, you get the plural facade of the content item. Then, to mark each item to be deleted, you call the Delete
method. Finally, you save the changes.