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.
SharePoint Online tutorial contents:
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:
- SharePoint 2016 Create and Deploy Visual web part using Visual Studio 2015
- Create Visual WebPart using Visual Studio in SharePoint 2013
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:
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.
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).
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.
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.
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“.
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.
You may like following SharePoint 2016 tutorials:
- Upload file to SharePoint 2016 document library with metadata programmatically using file upload control (Visual Web Part + Server Object Model)
- SharePoint 2013: Add, Delete and Display User Custom Actions using SharePoint Server Object Model
- [Solved] Trying to use an SPWeb Object that has been closed or disposed and is no longer valid SharePoint 2013/3016 Server Object Model
- Difference between client side object model and server side object model in SharePoint 2013/2016/Online
- How to get content database size using PowerShell in SharePoint 2016 and using the SharePoint server object model
- Create subsite using SharePoint 2016 server object model and SharePoint 2016 client object model
- the list is too large to save as a template. the size of a template cannot exceed 52428800 bytes
- [Solved] The list cannot be imported because a Microsoft SharePoint Foundation-compatible spreadsheet application is not installed or is not compatible with your browser
- SharePoint Online check if File exists or not in a document library using PowerShell
Conclusion
This SharePoint 2016 tutorial, we discussed how to create a document library programmatically c#.net using SharePoint 2016/2013 server object model code.
I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site EnjoySharePoint.com