Creating List And Columns Using SharePoint Hosted Add-ins

Introduction

 
In this blog, I have described step by step how to create a list and column using SharePoint self hosted Add-ins. To create the List and List Columns using SharePoint Hosted Apps, you have to follow the below steps.
 
Step 1 - Create a SharePoint Hosted App project.
Step 2 - Write HTML code under Default.aspx.
Step 3 - Add Javascript code under App.js file.
Step 4 - Deploy the Project.
 

Create a sharePoint Hosted App project

  • Open Visual Studio to create a SharePoint hosted Add-In. First, click the file menu option then click the new item. After that we can see the option for Project item, now click on the same option.
  • Expand the office/SharePoint tab and select SharePoint Add-in. Enter the project name & location of the project then hit the OK button.
  • Select the SharePoint site URL & Hosted add-in as shown below and then click on the Next button.
  • Now it will ask for your credentials. Enter the user Credentials (Username & Password) and Click the "Sign-in" Option.
  • Once the SharePoint hosted App is created, then we can add our code.
Write HTML code under Default.aspx
 
Go to Default.aspx page. Create a label and a button inside the div class as shown below.
  1. <tr>  
  2.          <td><label id="btnCreate" style="font-size:large;font-style:normal">Enter The List Name You Want To    Give</label></td>  
  3.          <td><input type="text" id="txtListName" name="txtListName" /></td>  
  4.          <td><input type="button" id="crtBtn" value="CreateList" /></td>  
  5.    </tr>   
Add Javascript code under App.js file
 
Go to App.js . Add your coding.
  1. function initializePage() {  
  2.     var context = SP.ClientContext.get_current();  
  3.     var user = context.get_web().get_currentUser();  
  4.     $(document).ready(function() {  
  5.                 $("#crtBtn").on('click'function() {  
  6.                     CreateList();  
  7.                 });  
  8.   
  9.                 function getURLParameters(param) {  
  10.                     var params = document.URL.split('?')[1].split('&');  
  11.                     var strParams = '';  
  12.                     var plength = params.length;  
  13.                     for (var i = 0; i < plength; i = i + 1) {  
  14.                         var singleParam = params[i].split('=');  
  15.                         if (singleParam[0] == param) {  
  16.                             return singleParam[1];  
  17.                         }  
  18.                     }  
  19.                 }  
  20.   
  21.                 function CreateList() {  
  22.                     var hostUrl = decodeURIComponent(getURLParameters("SPHostUrl"));  
  23.                     var clientContext = new SP.ClientContext.get_current();  
  24.                     var hostContext = new SP.AppContextSite(clientContext, hostUrl);  
  25.                     var oWeb = hostcontext.get_web();  
  26.                     var value = document.getElementById("txtListname").value.toLowerCase();  
  27.                     var listCreation = new SP.ListCreationInformation();  
  28.                     xlistCreation.set_title(value);  
  29.                     listCreation.set_templateType(SP.ListTemplateType.genericList);  
  30.                     var mySpList = oWeb.get_lists().add(listCreationInfo);  
  31.                     var fieldColl = mySpList.get_fields();  
  32.                     var spTxtField = fieldColl.addFieldAsXml('<Field Type="Text" DisplayName="Employee Name" Name="Employee Name" />'true, SP.AddFieldOptions.addToDefaultContentType);  
  33.                     spTxtField.set_title("Employee Name");  
  34.                     var spDescripField = fieldCol.addFieldAsXml('<Field Type="Note" DisplayName="Address" Name="Address" />'true, SP.AddFieldOptions.addToDefaultContentType);  
  35.                     spDescripField.set_title("Address");  
  36.                     mySpList.update();  
  37.                     clientContext.executeQueryAsync(function() {  
  38.                         alert("successfully Created The List and List Field");  
  39.                     }, function(sender, args) {  
  40.                         alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());  
  41.                     });  
  42.                 }  
  43.             }  
Deploy the Project
  • Go to the "Solution Explorer", right-click on the project and click on Deploy.
  • Once you deploy it, enter your office365 credentials.
  • Now you can see your App page.
  • Enter the list name and click on the CreateList button.
  • Now you can see the list in your sitecollection.
  • And the NewList which contains two columns as shown below.