Create A SharePoint List using REST API in SharePoint Online and Office 365


Welcome to an article on “How to Create a SharePoint List using REST API in SharePoint Online and Office 365” where we will see the steps of creating an app using Napa Tool which will help us to create as many SharePoint lists as we require 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?

    NAPA

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

    SharePoint Add-in

  • You will see the screen below on the app,

    Content

  • 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. function getQueryString(paramToRetrieve) {
    19.     var paramval = document.URL.split(“?”)[1].split(“&”);
    20.     for (var i = 0; i < paramval.length; i = i + 1) {
    21.         var paramval1 = paramval[i].split(“=”);
    22.         if (paramval1[0] == paramToRetrieve) return paramval1[1];
    23.     }
    24. }
    25. // Create a new custom list
    26. function createcustomlist() {
    27.     var listname = document.getElementById(“listnameid”).value;
    28.     var play;
    29.     // Initiate the request play 
    30.     play = new SP.RequestExecutor(applink);
    31.     play.executeAsync({
    32.         url: applink + “/_api/SP.AppContextSite(@target)/web/Lists?@target='” + hostweblink + “‘”,
    33.         method: “POST”,
    34.         body: “{ ‘__metadata’: { ‘type’: ‘SP.List’ }, ‘BaseTemplate’: 100,’Description’: ‘” + listname + “‘, ‘Title’:'” + listname + “‘}”,
    35.         headers: {
    36.             “content-type”“application/json; odata=verbose”
    37.         },
    38.         success: createlistcompleted,
    39.         error: createlistfailed
    40.     });
    41. }
    42. // createlistcompleted
    43. function createlistcompleted(data) {
    44.     alert(“Your List Created successfully”)
    45. }
    46. // createlistfailed
    47. function createlistfailed(data, errorCode, errorMessage) {
    48.     alert(“Your list creation failed “ + errorMessage);
    49. }
  • Click on the settings icon on the tool on the left.

    settings

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

    Permissions

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

    run project

  • Click on the launch button.

    Click on the launch button

  • Accept the trust and click on ‘Trust It’.

    Trust It

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

    Create your Custom List

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

    Create list

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

Today we saw how to create a SharePoint List using REST API in SharePoint Online and Office 365. You will love your app. Keep reading and keep learning!

One thought on “Create A SharePoint List using REST API in SharePoint Online and Office 365

  1. Pingback: Create A SharePoint List using REST API in SharePoint Online and Office 365 | SharePoint World by Manpreet

Leave a comment