Create List Using JavaScript in SharePoint 2013

1. Create a Separate page in SharePoint site and a content editor Webpart.

2. Upload the JavaScript code to site Assests and copy the link.

site Assests

3. Edit the CEWP and add the path of the script txt file and save it.

CEWP

4. Enter the list name and click the create list button and refresh the page.

5. The list created in SharePoint site.

Code:

  1. <script   
  2.     src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"   
  3.     type="text/javascript">  
  4. </script>  
  5. <script   
  6.     type="text/javascript"   
  7.     src="/_layouts/15/sp.runtime.js">  
  8. </script>  
  9. <script   
  10.     type="text/javascript"   
  11.     src="/_layouts/15/sp.js">  
  12. </script>  
  13.  <button onclick="createList()">Create List</button>   
  14. <input type="text" id="listnme" name="Enter the List Name" size="20">  
  15. <script type="text/javascript">  
  16. var siteUrl = 'https://thegowtham.sharepoint.com/teams/GT';  
  17.    function createList() {  
  18.     var clientContext = new SP.ClientContext(siteUrl);  
  19.     var oWebsite = clientContext.get_web();  
  20.       
  21.     var listCreationInfo = new SP.ListCreationInformation();  
  22.     var olist=document.getElementById("listnme").value;  
  23.     listCreationInfo.set_title(olist);  
  24.     listCreationInfo.set_templateType(SP.ListTemplateType.genericList);  
  25.   
  26.     this.oList = oWebsite.get_lists().add(listCreationInfo);  
  27.   
  28.     clientContext.load(oList);  
  29.     clientContext.executeQueryAsync(  
  30.         Function.createDelegate(thisthis.onQuerySucceeded),   
  31.         Function.createDelegate(thisthis.onQueryFailed)  
  32.     );  
  33. }  
  34.   
  35. function onQuerySucceeded() {  
  36.     var result = oList.get_title() + ' created.';  
  37.     alert(result);  
  38. }  
  39.   
  40. function onQueryFailed(sender, args) {  
  41.     alert('Request failed. ' + args.get_message() +   
  42.         '\n' + args.get_stackTrace());  
  43. }  
  44.   
  45. </script> 

Ebook Download
View all
Learn
View all