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>
Write the code, given below, on your *.js file-
-
- var context = SP.ClientContext.get_current();
-
-
- $(document).ready(function () {
- console.log("page loading...")
- $("#btnCreate").click(function () {
- createList();
- });
- });
- /this method is used to create list in host web
- function createList() {
-
- var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
-
- var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
-
- var currentWEB = appCtxSite.get_web();
-
- var listCreationInfo = new SP.ListCreationInformation();
- listCreationInfo.set_title("sampleList");
-
- listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
-
- 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());
- }
-
-
- 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];
- }
- }
- }
Note- In app manifest file, provide full control permission to the site collection, as shown below-
Summary
In this article, we have explored, how to create a list in SharePoint hosting Web, using JavaScript Object Model (JSOM).