Create a SharePoint Document Library with Columns programmatically using Visual Studio

In this SharePoint tutorial, we will discuss how to create a Document Library in SharePoint 2016/2013 programmatically using SharePoint 2016 server object model. Also, we will create a single line of text column and Choice column in the SharePoint 2016 Document Library using Server object model code.

Follow the step by step tutorial to create a document library in SharePoint 2016/2013 programmatically using SharePoint 2016 server object model. And steps by step add columns to the document library
programmatically in SharePoint 2016/2013.

Create Visual Web Part form document library in SharePoint programmatically

Now we will have to design the form where users can enter the details like Document library name and description etc to create the document library/ In the visual web part, we can design the form in the .ascx file. Here we will add below controls.

If you are new to SharePoint 2016 visual web part, you can check below tutorials:

Below are the controls we will use to develop the form.

  • TextBox: This control helps to create a text box in the input design form.
  • Button: This control helps to create a button. So that when a user click on button, the document library will create in the SharePoint 2016 Site.
  • Label: If some error will come after deploying the code, then this control helps to show the error message.
<table>
    <tr>
        <td>Document Library Name:</td>
        <td><asp:TextBox ID="txtDocumentLibraryName" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Description:</td>
        <td><asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine"></asp:TextBox></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnCreateDocumentLibrary" runat="server" Text="Create" Width="90px" OnClick="BtnCreateDocumentLibrary_Click" /></td>
    </tr>
    <tr>
        <td><asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></td>
    </tr>
</table>

The input design form looks like below:

create a document library programmatically in sharepoint 2016
create a document library programmatically in sharepoint 2016

Code to create document library in SharePoint programmatically

Below SharePoint 2016 server object model code to create SharePoint 2016 Document Library programmatically.

using Microsoft.SharePoint;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Document_Library_Creation.Document_Library_Creation
{
    public partial class Document_Library_CreationUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                {
            }
        }

        protected void BtnCreateDocumentLibrary_Click(object sender, EventArgs e)
        {
            CreateDocumentLibrary();
        }
        public void CreateDocumentLibrary()
        {
            try
            {
                SPSite siteCollection = SPContext.Current.Site;
                SPWeb web = SPContext.Current.Web;
                web.AllowUnsafeUpdates = true;
                SPList spList = web.Lists.TryGetList(txtDocumentLibraryName.Text.Trim());
                if (spList == null)
                {
                    web.Lists.Add(txtDocumentLibraryName.Text.Trim(), txtDescription.Text.Trim(), SPListTemplateType.DocumentLibrary);
                    //SPList newList = web.Lists["TestList"];
                    web.AllowUnsafeUpdates = false;
                    CreateColumn(web);
                    lblMessage.Text = "Your document library created successfully";
                }
                else
                {
                    lblMessage.Text = "This document library already exists";
                }

            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
    }
}

Once you will add your custom web part, you can see the form like below screenshot. Once you will fill all the field value and then click on the “Create” button, then the document library will create in SharePoint 2016.

programmatically create sharepoint document library c#
programmatically create sharepoint document library c#

While the document library will create successfully in SharePoint 2016, then there will appear a status as “Your document library created successfully” (because of Label control).

create a SharePoint document library programmatically using C#
create a SharePoint document library programmatically using C#

Once you will go to SharePoint 2016 “Site Contents” page, the document library which you have created recently will be available. Once you will open the document library, it will show you like below screenshot.

create SharePoint document library programmatically using C#
create SharePoint document library programmatically using C#

Create Column (Single line of text and Choice column)
programmatically in SharePoint 2016 Document Library

In this below example, we will see how can we add columns (Choice columns and Single line of text column) to the SharePoint 2016 document library. We are adding below 3 columns programmatically to SharePoint 2016 document library.

  • DocumentLocation (Single line of text): This column is a Single line of text.
  • DocumentLanguage (Choice column): This column is a Choice field. This language field column has some choice values as:
    • English
    • Hindi
    • Kannad
  • DocumentDepartment (Choice Column): This column is also a Choice field. This department field column has some choice values as:
    • IT
    • HR
    • Finance

Below SharePoint 2016 server object model Code, you can use for creating column ((Single line of text and Choice column) in SharePoint 2016 Document Library programmatically.

string libraryDocumentLocationColumnName = "DocumentLocation";
        string libraryLanguageColumnName = "DocumentLanguage";
        string languageChoiceColumn1 = "English";
        string languageChoiceColumn2 = "Hindi";
        string languageChoiceColumn3 = "Kannad";
        string libraryDepartmentColumnName = "DocumentDepartment";
        string departmentChoiceColumn1 = "IT";
        string departmentChoiceColumn2 = "HR";
        string departmentChoiceColumn3 = "Finance";

public void CreateColumn(SPWeb web)
        {
            try
            {
                web.AllowUnsafeUpdates = true;
                SPList spList = web.Lists.TryGetList

(txtDocumentLibraryName.Text.Trim());
                spList.Fields.Add

(libraryDocumentLocationColumnName, SPFieldType.Text, false);
                spList.Fields.Add(libraryLanguageColumnName, 

SPFieldType.Choice, false);
                spList.Fields.Add(libraryDepartmentColumnName, SPFieldType.Choice, false);
                
                /* get the newly added choice field instance */
                SPFieldChoice chFldDocLanguage = 

(SPFieldChoice)spList.Fields[libraryLanguageColumnName];
                
                /* set field format type i.e. radio / dropdown */
                chFldDocLanguage.EditFormat = 

SPChoiceFormatType.Dropdown;

                /* set the choice strings and update the field */
                chFldDocLanguage.Choices.Add

(languageChoiceColumn1);
                chFldDocLanguage.Choices.Add

(languageChoiceColumn2);
                chFldDocLanguage.Choices.Add

(languageChoiceColumn3);
                chFldDocLanguage.Update();
          
                /* get the newly added choice field instance */
                SPFieldChoice chFldDocDepartment = 

(SPFieldChoice)spList.Fields[libraryDepartmentColumnName];

                /* set field format type i.e. radio / dropdown */
                chFldDocDepartment.EditFormat = 

SPChoiceFormatType.Dropdown;

                /* set the choice strings and update the field */
                chFldDocDepartment.Choices.Add

(departmentChoiceColumn1);
                chFldDocDepartment.Choices.Add

(departmentChoiceColumn2);
                chFldDocDepartment.Choices.Add

(departmentChoiceColumn3);
                chFldDocDepartment.Update();
                spList.Update();
                web.AllowUnsafeUpdates = false;
            }
            catch (Exception exc)
            {
                lblMessage.Text = exc.Message;
            } 
        }

Now deploy the code and go to that existing SharePoint 2016 document library, then you can see that there will be available one single line of the text column and other choice columns as shown in below.

create column in sharepoint document library programmatically c#
create column in sharepoint document library programmatically c#

Once you will go to the “Columns” section of “Library Settings” of that SharePoint 2016 Document Library, then you can see all the Column name with their “Type“.

create column in sharepoint document library programmatically
create column in sharepoint document library programmatically

While you will click on the Choice Column name, then you can see all the choice field values in “Type each choice on a separate line” option as like below screenshot.

create column in sharepoint 2016 document library programmatically
create column in sharepoint 2016 document library programmatically

You may like following SharePoint 2016 tutorials:

Conclusion

This SharePoint 2016 tutorial, we discussed how to create a document library programmatically c#.net using SharePoint 2016/2013 server object model code.

Also we saw, how to create and add different types of columns in SharePoint 2016 Document Library. How to give choice field values programmatically using SharePoint 2016 server object model.

>