Steps to create list in SharePoint host web
Add HTML button, given below, in your *.aspx page-
- <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
- <div>
- <p id="message">
- <!-- The following content will be replaced with the user name when you run the app - see App.js -->
- <button type="button" id="btnCreate">Create SP List</button>
- </p>
- </div>
- </asp:Content>
- //get Current Context
- var context = SP.ClientContext.get_current();
- // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
- $(document).ready(function () {
- console.log("page loading...")
- $("#btnCreate").click(function () {
- createList();
- });
- });
- /this method is used to create list in host web
- function createList() {
- //Get host web URL
- var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- //get app context using host web url
- var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
- //get current web
- var currentWEB = appCtxSite.get_web();
- //create object for list creation
- var listCreationInfo = new SP.ListCreationInformation();
- listCreationInfo.set_title("sampleList");
- //provide template type - genericList is custom list template
- listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
- // add list in host web
- var list = currentWEB.get_lists().add(listCreationInfo);
- context.load(list);
- context.executeQueryAsync(function () {
- alert("Sharepoint custom list is created Successfully..")
- }, function (sender, args) {
- onfail(sender, args);
- });
- }
- / This function is executed if the above call fails
- function onfail(sender, args) {
- alert('Failed to create list. Error:' + args.get_message());
- }
- // this method used split the query string
- function manageQueryStringParameter(paramToRetrieve) {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) {
- return singleParam[1];
- }
- }
- }

Summary
In this article, we have explored, how to create a list in SharePoint hosting Web, using JavaScript Object Model (JSOM).

Join the conversation! Your thoughts help the community grow.