Share via


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?

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/NAPA.jpg

Select SharePoint Add-in and provide a name to your app and click on Create.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/Select%20SharePoint%20Add-in.jpg

You will see the screen below on the app,

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/Content.jpg

Click on Default.aspx and paste the code below under the “<asp:ContentContentPlaceHolderID="PlaceHolderMain" runat="server">”.

Code

  1. <p>  
  2.    <b>
  3.       Create Custom List</b>  
  4.    <br />  
  5.    <input type="text" value="Put the custom list name" id="listnameid" />  
  6.    <button id="createlistbtn">Create your Custom List</button>  
  7. </p>  

Now on the navigation click on the App.js file and paste the code below removing the previous code completely.

Code:

  1. 'use strict';  
  2. var hostweblink;  
  3. var applink;  
  4. // Get the links on load of the app  
  5. $(document).ready(function() {  
  6.     hostweblink = decodeURIComponent(  
  7.         getQueryString("SPHostUrl"));  
  8.     applink = decodeURIComponent(  
  9.         getQueryString("SPAppWebUrl"));  
  10.     //Get the function on the button click  
  11.     $("#createlistbtn").click(function(event) {  
  12.         createcustomlist();  
  13.         event.preventDefault();  
  14.     });  
  15.     var scriptlink = hostweblink + "/_layouts/15/";  
  16.     $.getScript(scriptlink + "SP.RequestExecutor.js");  
  17. });  
  18.   
  19. function getQueryString(paramToRetrieve) {  
  20.     var paramval = document.URL.split("?")[1].split("&");  
  21.     for (var i = 0; i < paramval.length; i = i + 1) {  
  22.         var paramval1 = paramval[i].split("=");  
  23.         if (paramval1[0] == paramToRetrieve) return paramval1[1];  
  24.     }  
  25. }  
  26. // Create a new custom list  
  27. function createcustomlist() {  
  28.     var listname = document.getElementById("listnameid").value;  
  29.     var play;  
  30.     // Initiate the request play   
  31.     play = new SP.RequestExecutor(applink);  
  32.     play.executeAsync({  
  33.         url: applink + "/_api/SP.AppContextSite(@target)/web/Lists?@target='" + hostweblink + "'",  
  34.         method: "POST",  
  35.         body: "{ '__metadata': { 'type': 'SP.List' }, 'BaseTemplate': 100,'Description': '" + listname + "', 'Title':'" + listname + "'}",  
  36.         headers: {  
  37.             "content-type": "application/json; odata=verbose"  
  38.         },  
  39.         success: createlistcompleted,  
  40.         error: createlistfailed  
  41.     });  
  42. }  
  43. // createlistcompleted  
  44. function createlistcompleted(data) {  
  45.     alert("Your List Created successfully")  
  46. }  
  47. // createlistfailed  
  48. function createlistfailed(data, errorCode, errorMessage) {  
  49.     alert("Your list creation failed " + errorMessage);  
  50. }  

Click on the settings icon on the tool on the left. 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/settings.jpg 

Under the properties, select Permissions and provide full control to the app on the Site Collection level.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/Permissions.jpg 

Click on the deploy button on the left and run the project.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/run%20project.jpg

Click on the launch button.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/Click%20on%20the%20launch%20button.jpg

Accept the trust and click on ‘Trust It’.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/Trust%20It.jpg

Your app will be deployed and open for you as per the following screenshot:

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/Create%20your%20Custom%20List.jpg

Provide a name of the list you want to create and click on ‘Create your Custom List’.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/create-a-sharepoint-list-using-rest-api-in-sharepoint-online/Images/Create%20list.jpg

Your list will be created by your own app and you can find the list under site contents on the site.