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. function getURLParameters(param) {
  9. var params = document.URL.split('?')[1].split('&');
  10. var strParams = '';
  11. var plength = params.length;
  12. for (var i = 0; i < plength; i = i + 1) {
  13. var singleParam = params[i].split('=');
  14. if (singleParam[0] == param) {
  15. return singleParam[1];
  16. }
  17. }
  18. }
  19. function CreateList() {
  20. var hostUrl = decodeURIComponent(getURLParameters("SPHostUrl"));
  21. var clientContext = new SP.ClientContext.get_current();
  22. var hostContext = new SP.AppContextSite(clientContext, hostUrl);
  23. var oWeb = hostcontext.get_web();
  24. var value = document.getElementById("txtListname").value.toLowerCase();
  25. var listCreation = new SP.ListCreationInformation();
  26. xlistCreation.set_title(value);
  27. listCreation.set_templateType(SP.ListTemplateType.genericList);
  28. var mySpList = oWeb.get_lists().add(listCreationInfo);
  29. var fieldColl = mySpList.get_fields();
  30. var spTxtField = fieldColl.addFieldAsXml('<Field Type="Text" DisplayName="Employee Name" Name="Employee Name" />', true, SP.AddFieldOptions.addToDefaultContentType);
  31. spTxtField.set_title("Employee Name");
  32. var spDescripField = fieldCol.addFieldAsXml('<Field Type="Note" DisplayName="Address" Name="Address" />', true, SP.AddFieldOptions.addToDefaultContentType);
  33. spDescripField.set_title("Address");
  34. mySpList.update();
  35. clientContext.executeQueryAsync(function() {
  36. alert("successfully Created The List and List Field");
  37. }, function(sender, args) {
  38. alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
  39. });
  40. }
  41. }
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.