Just copy the code below and paste it in a "content editor" webpart on a web part page.
- <script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>
- <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
- <script type="text/javascript" src="/_layouts/15/sp.js"></script> <input id="listName" type="text" name="Listname"></input> <input id="btnCreate" type="button" value="Click me" onclick="createList()">
- <script type="text/javascript">
- function createList() {
- var clientContext;
- var listCreationInfo;
- var web;
- var list;
- var lstname = document.getElementById("listName").value;
- clientContext = SP.ClientContext.get_current();
- web = clientContext.get_web();
- listCreationInfo = new SP.ListCreationInformation();
- listCreationInfo.set_title(lstname);
- listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
- list = web.get_lists().add(listCreationInfo);
- clientContext.load(list);
- clientContext.executeQueryAsync(function() {
- alert("List created successfully!")
- }, function() {
- alert("Failed to create list!")
- });
- }
- </script>
It's the working code. It will work 100% for you. I am using my SharePoint online account in this example. Just name the generic list in the text box you want to create then click on the Click me button as shown below.
A notification message will pop up as shown below.
You will find the newly created lists on the left navigation as shown below. If the list already exists it will give you a message saying that the list already exists.
It is a very basic example. I was looking for the working code but I was unable to find it anywhere, then after struggling with so many errors finally I was able to do that. I decided to post it so new programmers can simply copy paste the code directly and understand it.