# Create document libraries

When creating a document library, you must perform the following steps:

1. *Check whether the document library already exists.*

   Before you create the document library, you must check whether a document library with the same *ID* already exists.
2. *Create the document library.*

   If the document library does not exist, you create a new document library with the specified *ID*.
3. *Set the required properties.*

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

   * *Title*
   * *LastModified*
   * *DateCreated*
   * *UrlName*

   You can also set any other properties in this step.
4. *Save the document library.*

   Save all changes that you have made to the document library.

The example below shows you how to create a document library with predefined *ID*.

## Creating a document library with predefined ID

The following code creates a document library with the specified *ID* and *Title*.

**Native API**

```C#
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.DocumentsAndFiles.ManagingLibraries
{
    public partial class LibrariesSnippets
    {
        private void CreateDocumentLibraryNativeAPI(Guid documentLibraryId, string documentLibraryTitle)
        {
            LibrariesManager librariesManager = LibrariesManager.GetManager();
            DocumentLibrary library = librariesManager.GetDocumentLibraries().Where(b => b.Id == documentLibraryId).FirstOrDefault();

            if (library == null)
            {
                //Create the library.
                library = librariesManager.CreateDocumentLibrary(documentLibraryId);

                //Set the properties of the library.
                library.Title = documentLibraryTitle;
                library.DateCreated = DateTime.UtcNow;
                library.LastModified = DateTime.UtcNow;
                library.UrlName = Regex.Replace(documentLibraryTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

                //Recompiles and validates the url of the library.
                librariesManager.RecompileAndValidateUrls(library);

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

```

First, you get an instance of the *LibrariesManager* class. You check whether a document library with the same *ID* already exists. To create the document library, you call the CreateDocumentLibrary method of the *LibrariesManager* class and pass the specified *ID*. You can create a document library with either predefined or auto-generated *ID* depending on which overload of the method you use. Then, you set the properties of the document library object. It is recommended to set at least the following properties: *Title*, *UrlName*, *LastModified*,*DateCreated*. Finally, to save the document library, you call the SaveChanges method of the manager.

**Fluent API**

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.DocumentsAndFiles.ManagingLibraries
{
    public partial class LibrariesSnippets
    {
        private void CreateDocumentLibraryFluentAPI(Guid documentLibraryId, string documentLibraryTitle)
        {
            //Check whether a library with the same ID already exists.
            var count = 0;
            App.WorkWith().DocumentLibraries().Where(b => b.Id == documentLibraryId).Count(out count);

            if (count == 0)
            {
                //Create the library.
                App.WorkWith().DocumentLibrary().CreateNew(documentLibraryId)
                    //Set the properties of the library.
                .Do(b =>
                {
                    b.Title = documentLibraryTitle;
                    b.DateCreated = DateTime.UtcNow;
                    b.LastModified = DateTime.UtcNow;
                    b.UrlName = Regex.Replace(documentLibraryTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                })
                    //Save the changes.
                .SaveChanges();
            }
        }
    }
}

```

First, you check whether a document library with the same *ID* already exists. To create the document library, you call the CreateNewmethod of the singular document library facade. You can create a document library with either predefined or auto-generated *ID* depending on which overload of the method you use. To set the properties of the document library, you call the Do method of the facade. Finally, you save the changes.
