Create List in SharePoint 2013 Using REST API

Introduction

We can use the SharePoint 2013 Representational State Transfer (REST) service to do the same tasks you can do when you use the .Net CSOM and JSOM.

Here I will explain about CreateList in SharePoint 2013 using the REST API.

Procedure

  • On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project Create Site and then choose the Create button.

Prerequisites

These are important steps to be done before creating the app.

Specify the permissions that your app needs as in the following:

  • Choose the Properties button at the bottom of the page.

  • In the Properties window, choose Permissions.

  • In the Content category, set Write permissions for the Tenant scope.

  • In the Social category, set Read permissions for the User Profiles scope.

  • Close the Properties window.

Expand the Scripts node, choose the App.js file and delete the contents of the file and replace with the following code:

  1. 'use strict';  
  2. var hostweburl;   
  3. var appweburl;   
  4. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model   
  5. $(document).ready(function () {   
  6.    hostweburl= decodeURIComponent(getQueryStringParameter("SPHostUrl"));   
  7.    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));   
  8. });   
  9. function createSPList() {   
  10.    $.ajax(   
  11.    {   
  12.       url: appweburl +   
  13.       "/_api/SP.AppContextSite(@target)/web/lists?@target='" +   
  14.       hostweburl + "/sites/apps'",   
  15.       type: "POST",   
  16.       data: JSON.stringify({   
  17.       '__metadata': { 'type''SP.List' },   
  18.       'AllowContentTypes'true,   
  19.       'BaseTemplate': 100,   
  20.       'ContentTypesEnabled'true,   
  21.       'Description''My TestCustomList description',   
  22.       'Title''TestCustomList'   
  23.    }),   
  24.    headers: {   
  25.       "accept""application/json;odata=verbose",   
  26.       "content-type""application/json;odata=verbose",   
  27.       "X-RequestDigest": $("#__REQUESTDIGEST").val()   
  28.    },   
  29.    success: successHandler,   
  30.    error: errorHandler   
  31.    });   
  32. }   
  33. function successHandler() {   
  34.    $('#message').text('Success');   
  35. }   
  36. function errorHandler(data, errorCode, errorMessage) {   
  37.    $('#message').text('Error ' + errorMessage);   
  38. }   
  39. function getQueryStringParameter(paramToRetrieve) {   
  40.    var params =   
  41.    document.URL.split("?")[1].split("&");   
  42.    var strParams = "";   
  43.    for (var i = 0; i < params.length; i = i + 1) {   
  44.    var singleParam = params[i].split("=");   
  45.    if (singleParam[0] == paramToRetrieve)   
  46.       return singleParam[1];   
  47.    }   
  48. }  
Once you completed the code part then run the project.

Once you deployed the app select the Click here to launch your app in new window link. It will redirect to another page.

 



I hope you have enjoyed this.

Up Next
    Ebook Download
    View all
    Learn
    View all