Create lists

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

When creating a list, you must perform the following:

  1. Create new list.

    First, you must create new list.

  2. Set the required properties.

    When creating a new list, it is recommended to set at least the following properties:

    • Title
    • Description
    • UrlName
    • DateCreated

    You can also set any other properties in this step.

  3. Save the list.

    Save all changes that you have made to the list.

To create a list using the Native API, you must use the ListsManager class. For more information, see Creating a list using Native API.

To create a list using the Fluent API, you must use the list facade. For more information, see Creating a list using Fluent API.

The examples below show you how to create list with predefined ID.

Creating a list using Native API

The following code creates a list with the specified ID, Title and Description through the Native API.

C#
using System;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Lists.Model;
using Telerik.Sitefinity.Modules.Lists;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Lists
{
   public partial class ListsSnippets
   {
       public static void CreateListNativeAPI(Guid listId, string title, string description)
       {
           ListsManager listsManager = ListsManager.GetManager();

           // Create the list
           List list = listsManager.CreateList(listId);

           // Set the list properties
           list.Title = title;
           list.Description = description;
           list.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
           list.DateCreated = DateTime.Now;

           //Recompiles and validates the url of the list.
           listsManager.RecompileAndValidateUrls(list);

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

First, you initialize the ListsManager. You create the list using the CreateList method of the ListsManager class. Note that you can create a list with either predefined or auto-generated ID depending on which overload of the method you use. Then, you set the properties of the list. It is recommended to set at least the following properties: Title, Description, UrlName and DateCreated. Finally, you save the changes.

Creating a list using Fluent API

The following code creates a list with the specified ID, Title and Description through the Fluent API.

C#
using System;
using System.Text.RegularExpressions;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Lists
{
   public partial class ListsSnippets
   {
       public static void CreateListFluentAPI(Guid listId, string title, string description)
       {
           // Create the list
           App.WorkWith().List().CreateNew(listId)
               .Do(l =>
               {
                   // Set the list properties
                   l.Title = title;
                   l.Description = description;
                   l.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                   l.DateCreated = DateTime.Now;
               })
               .SaveChanges();
       }
   }
}

First, you initialize the singular facade of the list by calling App.WorkWith().List(). You create the list using the CreateNew method of the facade. Then, you set the properties of the list by calling the Do method of the facade. It is recommended to set at least the following properties: Title, Description, UrlName and DateCreated. 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.