Deleting content items

Sitefinity allows you to delete content items through the Content Blocks API.

NOTE: The code examples below work with the ID of the master version of the content item. 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.

When deleting a content item, you must perform the following:

  1. Get the content item.

    First, you must get the master version of the content item corresponds to the specified ID.

    For more information about querying content items, see Querying content items.

    For more information about finding specific content items, see Finding content items.

  2. Delete the content item.

    After you get the master version of the content item, you delete it.

  3. Save the changes.

    Finally, you must save the changes.

To delete a content item you can use the Native API or the Fluent API.

To delete a content item using the Native API, you must use the ContentManager class. For more information, see Deleting a content item using Native API.

To delete a content item using the Fluent API, you must use the content item facade. For more information, see Deleting a content item using Fluent API.

Deleting a content item using Native API

To delete a specific content item with the Native API, you use the ContentManager class and LINQ queries.

The following example deletes the content item with the specified Id:

public void DeleteContentItemNative(Guid masterContentItemId)
{
    ContentManager manager = ContentManager.GetManager();
  
    // Get the master version of the item.
    ContentItem contentItem = manager.GetContent().Where(cI => cI.Id == masterContentItemId).FirstOrDefault();
  
    if (contentItem != null)
    {
        // Mark the item to be deleted.
        manager.Delete(contentItem);
  
        // Save the changes.
        manager.SaveChanges();
    }
}

First, you initialize the ContentManager. Then, you get the master version of the content item using GetContent and filtering based on the Id. To delete the content item, you call the Delete method passing the master version as argument. Finally, you save the changes.

Deleting a content item using Fluent API

The following example deletes the content item with the specified Id:

public void DeleteContentItemFluent(Guid masterContentItemId)
{
    int count = 0;
    App.WorkWith().ContentItems().Where(cI => cI.Id == masterContentItemId).Count(out count);
  
    if (count > 0)
    {
        App.WorkWith().ContentItem(masterContentItemId).Delete().SaveChanges();
    }
}

First, you initialize the plural content item facade using App.WorkWith().ContentItems(). Then, you filter based on the Id to assure that the content item exists. To get the master version of the content item, you use the singular content item facade.

NOTE: If there is no content item with the specified Id, ContentItem(contentItemId) throws an exception of type ItemNotFoundException.

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

See Also

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