Deleting lists

Sitefinity allows you to delete lists through the Lists API.

When deleting a list, you must perform the following:

  1. Get the list.

    First, you get the list corresponding to the specified ID.

    For more information about querying lists, see Querying lists.

    For more information about finding specific lists, see Finding lists.

  2. Delete the list.

    After you get the list, you delete it.

  3. Save the list.

    Finally, you must save the changes.

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

Deleting a list by its ID

The following examples delete a list by its ID.

Native API

public void DeleteListNativeAPI(Guid listId)
{
    ListsManager listsManager = ListsManager.GetManager();
 
    // Get the list
    List list = listsManager.GetLists().Where(l => l.Id == listId).FirstOrDefault();
 
    if (list != null)
    {
        // Mark the item to be deleted
        listsManager.DeleteList(list);
 
        // Save the changes
        listsManager.SaveChanges();
    }
}

First, you initialize the ListsManager. Then, you get the list using GetLists and filtering based on the Id property.

For more information about querying lists, see Querying lists.

For more information about finding specific lists, see Finding lists.

To delete the list, you call the DeleteList method. Finally, you save the changes.

Fluent API

public void DeleteListFluentAPI(Guid listId)
{
    int count = 0;
    App.WorkWith().Lists().Where(l => l.Id == listId).Count(out count);
 
    if (count > 0)
    {
        App.WorkWith().List(listId).Delete().SaveChanges();
    }
}

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

For more information about querying lists, see Querying lists.

For more information about finding specific lists, see Finding lists.

To get the list, you use the singular facade.

NOTE: If there is no list with the specified Id, List(listId) throws an exception of type ItemNotFoundException.

To delete the list, you use the Delete method of the facade. 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