Delete list items

Sitefinity CMS allows you to delete list items through the Lists API.

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

  1. Get the list item.

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

    For more information about querying list items, see For developers: Query list items

    For more information about finding specific list items, see For developers: Find list items.

  2. Delete the list item.

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

  3. Save the changes.

    Finally, you must save the changes.

To delete a list item 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 list 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 For developers: Delete content.

Deleting a list item by its ID

The following examples delete a list item by the ID of its master version.

Native API


C#
using System;
using System.Linq;
using Telerik.Sitefinity.Lists.Model;
using Telerik.Sitefinity.Modules.Lists;

namespace SitefinityWebApp
{
    public class DeleteListItems_DeleteListItemNativeAPI
    {
        public void DeleteListItemNativeAPI(Guid masterListItemId)
        {
            ListsManager listsManager = ListsManager.GetManager();

            // Get the master version of the list item
            ListItem listItem = listsManager.GetListItems().Where(i => i.Id == masterListItemId).FirstOrDefault();

            if (listItem != null)
            {
                // Mark the list item to be deleted
                listsManager.DeleteListItem(listItem);

                // Save the changes
                listsManager.SaveChanges();
            }
        }
    }
}

First, you initialize the ListsManager. Then, you get the master version of the list item using GetListItems and filtering based on the Idproperty.

For more information about querying list items, see For developers: Query list items

For more information about finding specific list items, see For developers: Find list items.

To delete the list item, you call the DeleteListItem method passing the master version as argument. Finally, you save the changes.

Fluent API

C#
using System;
using Telerik.Sitefinity;

namespace SitefinityWebApp
{
    public class DeleteListItems_DeleteListItemFluentAPI
    {
        public void DeleteListItemFluentAPI(Guid masterListItemId)
        {
            int count = 0;
            App.WorkWith().ListItems().Where(i => i.Id == masterListItemId).Count(out count);

            if (count > 0)
            {
                App.WorkWith().ListItem(masterListItemId).Delete().SaveChanges();
            }
        }
    }
}

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

For more information about querying list items, see For developers: Query list items

For more information about finding specific list items, see For developers: Find list items.

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

**NOTE:**If there is no list item with the specified Id, ListItem(masterListItemId) throws an exception of type ItemNotFoundException.

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

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
This Article Contains
New to Sitefinity?