SharePoint Online and Office 365: Create A SharePoint List using REST API
Open the “NAPA” Office 365 Development Tools through your SharePoint store.
Click on Add New Project.
It will ask you, what type of app do you want to build?
Select SharePoint Add-in and provide a name to your app and click on Create.
You will see the screen below on the app,
Click on Default.aspx and paste the code below under the “<asp:ContentContentPlaceHolderID="PlaceHolderMain" runat="server">”.
Code
- <p>
- <b>
- Create Custom List</b>
- <br />
- <input type="text" value="Put the custom list name" id="listnameid" />
- <button id="createlistbtn">Create your Custom List</button>
- </p>
Now on the navigation click on the App.js file and paste the code below removing the previous code completely.
Code:
- 'use strict';
- var hostweblink;
- var applink;
- // Get the links on load of the app
- $(document).ready(function() {
- hostweblink = decodeURIComponent(
- getQueryString("SPHostUrl"));
- applink = decodeURIComponent(
- getQueryString("SPAppWebUrl"));
- //Get the function on the button click
- $("#createlistbtn").click(function(event) {
- createcustomlist();
- event.preventDefault();
- });
- var scriptlink = hostweblink + "/_layouts/15/";
- $.getScript(scriptlink + "SP.RequestExecutor.js");
- });
- function getQueryString(paramToRetrieve) {
- var paramval = document.URL.split("?")[1].split("&");
- for (var i = 0; i < paramval.length; i = i + 1) {
- var paramval1 = paramval[i].split("=");
- if (paramval1[0] == paramToRetrieve) return paramval1[1];
- }
- }
- // Create a new custom list
- function createcustomlist() {
- var listname = document.getElementById("listnameid").value;
- var play;
- // Initiate the request play
- play = new SP.RequestExecutor(applink);
- play.executeAsync({
- url: applink + "/_api/SP.AppContextSite(@target)/web/Lists?@target='" + hostweblink + "'",
- method: "POST",
- body: "{ '__metadata': { 'type': 'SP.List' }, 'BaseTemplate': 100,'Description': '" + listname + "', 'Title':'" + listname + "'}",
- headers: {
- "content-type": "application/json; odata=verbose"
- },
- success: createlistcompleted,
- error: createlistfailed
- });
- }
- // createlistcompleted
- function createlistcompleted(data) {
- alert("Your List Created successfully")
- }
- // createlistfailed
- function createlistfailed(data, errorCode, errorMessage) {
- alert("Your list creation failed " + errorMessage);
- }
Click on the settings icon on the tool on the left.
Under the properties, select Permissions and provide full control to the app on the Site Collection level.
Click on the deploy button on the left and run the project.
Click on the launch button.
Accept the trust and click on ‘Trust It’.
Your app will be deployed and open for you as per the following screenshot:
Provide a name of the list you want to create and click on ‘Create your Custom List’.
Your list will be created by your own app and you can find the list under site contents on the site.