Here I am explaining about list and library creation using SharePoint Server Object model and Client Object Model in SharePoint.
Introduction
Microsoft has already provided out of box features to create a list or library in SharePoint. If we want more customization then we need the custom coding to do this.
We can create a list in many ways .
- Using Server Object Model ( Visual Studio Required )
- Using Client Object Model
- Using Out Of Box Features
Using Server Object Model ( Visual Studio Required )
We need to install Visual Studio in our machine for creating a List using SharePoint Server Object Model. The Server Object Model will be executed in the server side & it provides a rich set of classes in representing & manipulating SharePoint objects. Server object model is like production server environment to access the data where Sharepoint is installed on machine .
Server Object Model is the most extensive API set available for SharePoint 2013. The core assembly is Microsoft.SharePoint.dll which is installed in the Global Assembly Cache.
The general classes are available in the Microsoft.SharePoint namespace and the administration classes inside the Microsoft.SharePoint.Administration namespace.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Microsoft.SharePoint.Client;
-
- namespace WebApplication
- {
- public partial class ListCreation : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- using (SPSite oSPsite = new SPSite("https://sharepoint.com/sites/TestSite"))
- {
-
- using (SPWeb oSPWeb = oSPsite.OpenWeb())
- {
- oSPWeb.AllowUnsafeUpdates = true;
-
- SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
- SPListTemplate template = lstTemp["custom template name"];
- oSPWeb.Lists.Add("TestList", "Description", template);
- SPList newList = web.Lists["TestList"];
- oSPWeb.AllowUnsafeUpdates = false;
-
- }
-
- }
-
- }
- }
- }
Create a List using Javascript
The JavaScript object model is defined in a set of *.js files located at %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS on each server.
We need to add the reference file in the top of the page,
- <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
- <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
- <script type="text/javascript" src="/_layouts/15/sp.js"></script>
- <script type="text/javascript">
- function createList()
- {
- var siteUrl="https://sharepoint.com/sites/TestSite";
- var clientContext = new SP.ClientContext(siteUrl);
- var oWebsite = clientContext.get_web();
- alert(oWebsite);
- var listCreationInfo = new SP.ListCreationInformation();
- listCreationInfo.set_title('TestList');
- listCreationInfo.set_templateType(SP.ListTemplateType."custom template name");
- this.oList = oWebsite.get_lists().add(listCreationInfo);
- clientContext.load(oList);
- clientContext.executeQueryAsync(
- Function.createDelegate(this, this.onQuerySucceeded),
- Function.createDelegate(this, this.onQueryFailed)
- );
- }
-
- function onQuerySucceeded()
- {
- alert("result");
- var result = oList.get_title() + ' created.';
- alert(result);
- }
-
- function onQueryFailed(sender, args)
- {
- alert('Request failed. ' + args.get_message() +
- '\n' + args.get_stackTrace());
- }
- </script>
Create List Using Client Object Model
There are two assemblies to be referred to for working with the Client Object Model.
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
These assemblies can be found in the 15 Hive folder: %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI.
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- namespace CreateTestList
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
-
- ClientContext clientContext = new ClientContext("https://sharepoint.com/sites/TestSite");
-
-
- ListCreationInformation creationInfo = new ListCreationInformation();
- creationInfo.Title = "TestList";
- creationInfo.Description = "Create TestList";
- creationInfo.TemplateType = (int) ListTemplateType.GenericList;
-
-
- List newList = clientContext.Web.Lists.Add(creationInfo);
-
- clientContext.Load(newList);
-
- clientContext.ExecuteQuery();
-
- Console.WriteLine(newList.Title);
- Console.ReadLine();
- }
- }
- }
Using Out Of Box Features
Go to Your Site -> Add an app-> Create a Custom List
Click on Custom List -> New popup will come -> Enter the Name of your List -> Create
Next look into the below image, Your newly created list is visible over here.
These are the process for create a list in SharePoint 2013,2010 or SharePoint Online.
Same you can also create Document Library in SharePoint Online Using Server and Client Object model plus Out of Box features.
Using Server Object Model ( Visual Studio Required )
Server Object Model is the most extensive API set available for SharePoint 2013. The core assembly is Microsoft.SharePoint.dll which is installed in the Global Assembly Cache.
The general classes are available in the Microsoft.SharePoint namespace and the administration classes inside the Microsoft.SharePoint.Administration namespace.
- SPSite mySite = new SPSite("https://sharepoint.com/sites/TestSite");
- SPWeb myWeb = mySite.openWeb();
- myWeb.AllowUnSafeUpdates = true;
- myWeb.Lists.Add("TestLibrary","Test Document Library", SPListTemplateType.DocumentLibrary);
- myWeb.Update();
- myWeb.AllowUnsafeUpdates = false;
- myWeb.Dispose();
- mySite.Dispose();
Create a Library using Javascript
The JavaScript object model is defined in a set of *.js files located at %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS on each server.
We need to add the reference file in top of the page.
- <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
- <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
- <script type="text/javascript" src="/_layouts/15/sp.js"></script>
- <script type="text/javascript">
- var context = SP.ClientContext.get_current();
- var web = context.get_web();
- var list = web.get_lists();
- var docLibCreation;
-
- function createDocLib() {
- docLibCreation = new SP.ListCreationInformation();
- docLibCreation.set_title("TestLibrary");
- docLibCreation.set_templateType(SP.ListTemplateType.documentLibrary);
- list.add(docLibCreation)
- context.load(list);
- context.executeQueryAsync(onDocLibCreationSuccess, onDocLibCreationFail);
- }
-
- function onDocLibCreationSuccess() {
- alert(docLibCreation.title + "Created");
- }
-
- function onDocLibCreationFail(sender, args) {
- alert('Failed to Create the Document Library. Error:' + args.get_message());
- }
- </script>
Create Library Using Client Object Model
There are two assemblies to be referred to for working with the Client Object Model.
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
These assemblies can be found in the 15 Hive folder: %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI.
- using (ClientContext clientCTX = new ClientContext(url))
- {
- ListCreationInformation Information = new ListCreationInformation();
- Information.Description = "Test Document Library";
- Information.Title = "TestLibrary";
- Information.TemplateType = 101;
- List newLib = clientCTX.Web.Lists.Add(Information);
- clientCTX.Load(newLib);
- clientCTX.ExecuteQuery();
- }
Using Out Of Box Features
Go to Your Site -> Add an app-> Create a Document Library
Click on Document Library -> New popup will come -> Enter the Name of your Library-> Create
Next look into the below image, Your newly cretaed library is visible over here.
These are all the steps for creating a List and Library In SharePoint.