How to Create SharePoint List or Library Programmatically?

At times, We may have to create List or Document Libraries programmatically. These code snippets will help to get it done.

Create a Document Library in SharePoint Using .Net Object Model (C#)

using (SPSite site = new SPSite("https://sharepoint"))
{
	using (SPWeb web = site.OpenWeb())
	{
		//Create a document library
		web.Lists.Add("Project Docs", "Library for Storing Project Documents", SPListTemplateType.DocumentLibrary);

		//Create a Link List
		web.Lists.Add("Portal Links", "Links to Portal Resources", SPListTemplateType.Links);
		//SPListTemplateType.Links can be: SPListTemplate AssetTemplate = web.ListTemplates["Links"];

		//Create a List based on Custom List template
		SPListTemplateCollection CustomListTemplates = site.GetCustomListTemplates(web);
		SPListTemplate TrainingTemplate = CustomListTemplates["Training List Template"];
		web.Lists.Add("Project Links", "Links to project artifacts", TrainingTemplate);

		//Set the Show in Quick Lanuch Property
		SPList ProjectLinks = web.Lists["Project Links"];
		ProjectLinks.OnQuickLaunch = true;

		// Add new column to the List
		ProjectLinks.Fields.Add("Link Description", SPFieldType.Text,true);
		ProjectLinks.Update();

		// If the "Notes"  Rich Text Field field, if is not in the List already!
		SPFieldCollection fields = ProjectLinks.Fields;
		if (!fields.ContainsField("Notes"))
		{
			//Add Notes field Rich Text Field
			SPFieldMultiLineText oFldNotes = (SPFieldMultiLineText)CaseNotes.Fields[CaseNotes.Fields.Add("Notes", SPFieldType.Note, true)];
			oFldNotes.RichText = true;
			oFldNotes.RichTextMode = SPRichTextMode.FullHtml;
			oFldNotes.Update();
			//Update the List
			ProjectLinks.Update();
		}
	}
}

For all available List template types, Refer: SPListTemplateType

Create a SharePoint List using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
$site = Get-SPSite -Identity "https://sharepoint"
$web= $site.OpenWeb()

#Create a Link List
$LinkTemplate = $web.ListTemplates["Links"];
$web.Lists.Add("Support Links", "Links to Portal support", $LinkTemplate);
#$LinkTemplate can be: $LinkTemplate = [Microsoft.SharePoint.SPListTemplateType]::Links

#set Quick lanuch option
$SupportLinkList = $web.Lists["Support Links"]
$SupportLinkList.OnQuickLaunch = $true
#Add new Field
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$SupportLinkList.Fields.Add("Link Description",$spFieldType,$true)
$SupportLinkList.update()

#Create a List based on Custom List template
$CustomListTemplates = $site.GetCustomListTemplates($web)
$web.Lists.Add("Project Links", "Links to project artifacts", $CustomListTemplates["Training List Template"])

$web.Dispose()
$site.dispose()

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

2 thoughts on “How to Create SharePoint List or Library Programmatically?

  • Hi ,
    Can anyone please tell me what is SPSite here.

    Thanks,
    Manoj

    Reply
    • Manoj,
      SPSite is the Class/Object for handling SharePoint “Site Collection” programmatically. You can Retrieve/Manipulate Site collection objects with SPSite.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *